contextly-8.x-2.1/src/MetaGenerator.php
src/MetaGenerator.php
<?php
namespace Drupal\contextly;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\file\FileInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\user\UserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* The MetaGenerator class.
*/
class MetaGenerator implements MetaGeneratorInterface {
const CONTEXTLY_VERSION = '4.0';
/**
* Map definition.
*
* @var array
* The map array.
*/
protected $map;
/**
* The Drupal\Core\Entity\ContentEntityInterface definition.
*
* @var \Drupal\Core\Entity\ContentEntityInterface
* The entity which generate metatags.
*/
protected $entity;
/**
* The Drupal\user\UserInterface definition.
*
* @var \Drupal\user\UserInterface
* The user account.
*/
protected $account;
/**
* Define meta tags array.
*
* @var array
* The meta tags array.
*/
protected $metaTags = [];
/**
* {@inheritdoc}
*/
public function __construct(
protected readonly ContainerInterface $container,
) {
$this->map = $this->defaultMap();
}
/**
* {@inheritdoc}
*/
public function setEntity(ContentEntityInterface $entity): void {
$this->entity = $entity;
$this->account = $entity->get('uid')->entity;
}
/**
* {@inheritdoc}
*/
public function getEntity(): ?EntityInterface {
return $this->entity;
}
/**
* {@inheritdoc}
*/
public function getAccount():? UserInterface {
return $this->account;
}
/**
* {@inheritdoc}
*/
public function setMetaMap(array $map) {
$this->map = $map;
}
/**
* {@inheritdoc}
*/
public function getMetaMap(): array {
return $this->map;
}
/**
* {@inheritdoc}
*/
public function createMetaTags(ContentEntityInterface $entity) {
$this->setEntity($entity);
$this->metaTags = [];
foreach ($this->map as $meta_key => $definition) {
if (is_array($definition)) {
[$function, $class] = $definition;
}
else {
$function = $definition;
$class = $this;
}
call_user_func([$class, $function], $meta_key);
}
// Let modules modify meta tags.
$this->container->get('module_handler')
->alter('contextly_meta_tags', $this->metaTags, $this);
}
/**
* {@inheritdoc}
*/
public function getMetaTags(): array {
return $this->metaTags;
}
/**
* {@inheritdoc}
*/
public function setTitle(string $key) {
$this->metaTags[$key] = $this->entity->label();
}
/**
* {@inheritdoc}
*/
public function setUri(string $key) {
$this->metaTags[$key] = $this->entity
->toUrl('canonical', ['absolute' => TRUE])->toString();
}
/**
* {@inheritdoc}
*/
public function setCreatedDate(string $key) {
$this->metaTags[$key] = date('Y-m-d H:i:s',
$this->entity->get('created')->getString());
}
/**
* {@inheritdoc}
*/
public function setUpdatedDate(string $key) {
$this->metaTags[$key] = date('Y-m-d H:i:s',
$this->entity->get('changed')->getString());
}
/**
* {@inheritdoc}
*/
public function setType(string $key) {
$this->metaTags[$key] = 'post';
}
/**
* {@inheritdoc}
*/
public function setId(string $key) {
$this->metaTags[$key] = $this->entity->id();
}
/**
* {@inheritdoc}
*/
public function setAuthorId(string $key) {
$this->metaTags[$key] = $this->getAccount()->id();
}
/**
* {@inheritdoc}
*/
public function setAuthorName(string $key) {
$this->metaTags[$key] = $this->getAccount()->getAccountName();
}
/**
* {@inheritdoc}
*/
public function setAuthorDisplayName(string $key) {
$this->metaTags[$key] = $this->getAccount()->getDisplayName();
}
/**
* {@inheritdoc}
*/
public function setTags(
string $key,
string $field_name = 'field_tags') {
$this->metaTags[$key] = $this->getReferencedLabels($field_name);
}
/**
* {@inheritdoc}
*/
public function setCategories(
string $key,
string $field_name = 'field_category') {
$this->metaTags[$key] = $this->getReferencedLabels($field_name);
}
/**
* {@inheritdoc}
*/
public function setImage(
string $key,
string $field_name = 'field_image',
ContentEntityInterface $entity = NULL) {
$url = '';
$alt = '';
if (empty($entity)) {
$entity = $this->entity;
}
if ($entity->hasField($field_name) && !$entity->get($field_name)->isEmpty()) {
/** @var \Drupal\file\FileInterface $image */
$image = $entity->get($field_name)->entity;
if ($image instanceOf FileInterface) {
/** @var \Drupal\Core\File\FileUrlGeneratorInterface $generator */
$generator = $this->container->get('file_url_generator');
$url = $generator->generateAbsoluteString($image->getFileUri());
$alt = $entity->get($field_name)->getValue()[0]['alt'];
}
}
$this->metaTags[$key] = $url;
if (!empty($alt)) {
$this->metaTags['image_alt'] = $alt;
}
}
/**
* {@inheritdoc}
*/
public function getApiData(): ? array {
$api_key = $this->container->get('config.factory')
->get('contextly.settings')->get('api_key');
return explode('-', $api_key);
}
/**
* Prepare elements array.
*
* @param string $field_name
* The field name.
*
* @return array
* The elements array.
*/
protected function getReferencedLabels(string $field_name): array {
$elements = [];
if (!$this->entity->get($field_name)->isEmpty()) {
/** @var \Drupal\Core\Entity\EntityInterface $entity */
foreach ($this->entity->{$field_name}->referencedEntities() as $entity) {
$elements[] = $entity->label();
}
}
return $elements;
}
/**
* Add app id to meta tags.
*
* @param string $key
* The key from meta tag map.
*/
protected function setAppId(string $key) {
[$app_id] = $this->getApiData();
$this->metaTags[$key] = $app_id;
}
/**
* Add version to meta tags.
*
* @param string $key
* The key from meta tag map.
*/
protected function setVersion(string $key) {
$this->metaTags[$key] = $this::CONTEXTLY_VERSION;
}
/**
* Return the default contextly meta tag map.
*
* @return array
* The default meta tag map.
*/
protected function defaultMap(): array {
return [
'title' => 'setTitle',
'url' => 'setUri',
'pub_date' => 'setCreatedDate',
'mod_date' => 'setUpdatedDate',
'type' => 'setType',
'post_id' => 'setId',
'author_id' => 'setAuthorId',
'author_name' => 'setAuthorName',
'author_display_name' => 'setAuthorDisplayName',
'tags' => 'setTags',
'categories' => 'setCategories',
'image' => 'setImage',
'app_id' => 'setAppId',
'version' => 'setVersion',
];
}
}
