Programming Tips

This page contains some commonly asked programming tips on how to use the Multiple Domains API.

For all of the examples, you will need access to the $app variable:

$app = \Concrete\Core\Support\Facade\Application::getFacadeApplication();

The mapping object itself is a Doctrine entity. We extend the package's own entity class to get an easy access to the entity's properties through the ->get() method. For example:

// You can see the available properties from /src/Entity/Mapping.php
echo t("Domain's ID: %d", $domain->get('mID'));
echo t("Domain is: %s", $domain->get('domain'));
echo t("Domain's name is: %s", $domain->get('name'));

Get Domain Object by Address

$dh = $app->make('multiple_domains/helper/domains');
$mapping = $dh->findMappingByDomain('www.yourdomain.com');
if (is_object($mapping)) {
    echo t("The domain %s is mapped with mapping ID %d.", $mapping->get('domain'), $mapping->get('mID'));
}

Get Current Domain Object

$dh = $app->make('multiple_domains/helper/domains');
$mapping = $dh->findMappingByCurrentDomain();
if (is_object($mapping)) {
    echo t("Currently requesting a domain that is mapped to mapping with ID: %d", $mapping->get('mID'));
}

For Which Domain a Page Belongs To?

$dh = $app->make('multiple_domains/helper/domains');
$mapping = $dh->findMappingBySectionOfSite($targetPage);
if (is_object($mapping)) {
    echo t("The page with ID %d belongs to domain %s.", $targetPage->getCollectionID(), $mapping->get('domain'));
}