PHP code example of heimrichhannot / contao-head-bundle
1. Go to this page and download the library: Download heimrichhannot/contao-head-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
heimrichhannot / contao-head-bundle example snippets
use HeimrichHannot\HeadBundle\HeadTag\BaseTag;
use HeimrichHannot\HeadBundle\HeadTag\MetaTag;
use HeimrichHannot\HeadBundle\HeadTag\TitleTag;
use HeimrichHannot\HeadBundle\HeadTag\Meta\CharsetMetaTag;
use HeimrichHannot\HeadBundle\HeadTag\Meta\HttpEquivMetaTag;
use HeimrichHannot\HeadBundle\HeadTag\Meta\PropertyMetaTag;
use HeimrichHannot\HeadBundle\Manager\HtmlHeadTagManager;
use Symfony\Component\HttpFoundation\Request;
class SomeEventListener
{
private HtmlHeadTagManager $headTagManager;
public function updateBaseTag(Request $request): void
{
// Set base tag to null to remove it
$this->headTagManager->setBaseTag(null);
//Set base tag from object or url
$this->headTagManager->setBaseTag(new BaseTag($request->getSchemeAndHttpHost()));
$this->headTagManager->setBaseTag('https://example.org'));
}
public function updatedTitleTag(): void
{
// Set title to "Hello World"
$this->headTagManager->setTitleTag('Hello World');
// Set title tag from object and adjust output format
$this->headTagManager->setTitleTag(new TitleTag('Foo Bar', '%s | {{page::rootPageTitle}}'))
// Will output: <title>Foo Bar | My Great Website Page Title</title>
}
public function setMetaTags(): void
{
// Add a new meta tag. If a tag with the same name already exists, it will be overridden
$this->headTagManager->addMetaTag(new MetaTag('author', 'John Doe'));
// Get an existing tag
$description = ($tag = $this->headTagManager->getMetaTag('og:description')) ? $tag->getContent() : '';
// Remove a tag
$this->headTagManager->removeMetaTag('twitter:site');
// Create a tag for property meta tags
$this->headTagManager->addMetaTag(new PropertyMetaTag('og:type', 'article'));
// Create a http-equiv tag
$this->headTagManager->addMetaTag(new HttpEquivMetaTag('refresh', '30'));
// Set a charset tag
$this->headTagManager->addMetaTag(new CharsetMetaTag('UTF-8'));
// Create tags without class (usefull when creating tags in a loop without custom checks)
$this->headTagManager->addMetaTag(
$this->headTagManager->getHeadTagFactory()->createMetaTag('description', 'Lorem ipsum!')
);
$this->headTagManager->addMetaTag(
$this->headTagManager->getHeadTagFactory()->createTagByName('meta_og:url', 'https://example.org')
);
}
public function setLinkTags(): void
{
// Add a new link tag. If a tag with the same name already exists, it will be overridden
$this->headTagManager->addLinkTag(new LinkTag('next', 'https://example.org?page=2'));
// Get an existing tag
$this->headTagManager->getLinkTag('prev');
// Remove a tag
$this->headTagManager->removeLinkTag('prev');
// Shorthand for canonical tag
$this->headTagManager->setCanonical('https://example.org');
}
}
use HeimrichHannot\HeadBundle\Manager\JsonLdManager;
class ExampleController
{
private JsonLdManager $jsonLdManager;
public function __invoke() {
$organisation = $this->jsonLdManager->getGraphForSchema(JsonLdManager::SCHEMA_ORG)->organization();
$organisation->name('Example and Sons Ltd.');
$organisation->url('https://example.org');
}
}
use HeimrichHannot\HeadBundle\Helper\DcaHelper;
class HeadTagOptionsListener {
private DcaHelper $dcaHelper;
public function __invoke() {
return $this->dcaHelper->getTagOptions([
// filter: (array|null) If set, only tags fulfill given filters will be returned. See FILTER constants for available options. Default null
'filter' => [DcaHelper::FILTER_META, DcaHelper::FILTER_TITLE],
// skip_tags: (array) Skip specific tags. Default empty
'skip_tag' => ['og:locale'],
]);
}
}
use Contao\ContentModel;
use HeimrichHannot\HeadBundle\Manager\HtmlHeadTagManager;
class SomeEventListener {
private HtmlHeadTagManager $headTagManager;
public function __invoke(ContentModel $contentModel){
$tag = $this->headTagManager->getHeadTagFactory()->createTagByName($contentModel->headTag);
if ($tag) {
$tag->setAttribute("content", $contentModel->headTagContent);
$this->headTagManager->addTag($tag);
}
}
}
$this->block('meta');
<meta charset="<?= $this->charset
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.