use Drupal\Component\Render\PlainTextOutput;
use Drupal\Core\Entity\ContentEntityInterface;
-use Drupal\Core\Entity\EntityTypeManager;
+use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
-use Drupal\field\Entity\FieldConfig;
use Drupal\metatag\Entity\MetatagDefaults;
use Drupal\views\ViewEntityInterface;
*/
class MetatagManager implements MetatagManagerInterface {
+ /**
+ * @var \Drupal\metatag\MetatagGroupPluginManager
+ */
protected $groupPluginManager;
+
+ /**
+ * @var \Drupal\metatag\MetatagTagPluginManager
+ */
protected $tagPluginManager;
+
+ /**
+ * @var array
+ */
protected $metatagDefaults;
+
+ /**
+ * @var \Drupal\metatag\MetatagToken
+ */
protected $tokenService;
/**
/**
* Constructor for MetatagManager.
*
- * @param MetatagGroupPluginManager $groupPluginManager
- * @param MetatagTagPluginManager $tagPluginManager
- * @param MetatagToken $token
- * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $channelFactory
- * @param EntityTypeManager $entityTypeManager
+ * @param Drupal\metatag\MetatagGroupPluginManager $groupPluginManager
+ * The MetatagGroupPluginManager object.
+ * @param Drupal\metatag\MetatagTagPluginManager $tagPluginManager
+ * The MetatagTagPluginMÏ€anager object.
+ * @param Drupal\metatag\MetatagToken $token
+ * The MetatagToken object.
+ * @param Drupal\Core\Logger\LoggerChannelFactoryInterface $channelFactory
+ * The LoggerChannelFactoryInterface object.
+ * @param Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
+ * The EntityTypeManagerInterface object.
*/
public function __construct(MetatagGroupPluginManager $groupPluginManager,
- MetatagTagPluginManager $tagPluginManager,
- MetatagToken $token,
- LoggerChannelFactoryInterface $channelFactory,
- EntityTypeManager $entityTypeManager) {
+ MetatagTagPluginManager $tagPluginManager,
+ MetatagToken $token,
+ LoggerChannelFactoryInterface $channelFactory,
+ EntityTypeManagerInterface $entityTypeManager) {
$this->groupPluginManager = $groupPluginManager;
$this->tagPluginManager = $tagPluginManager;
$this->tokenService = $token;
$this->metatagDefaults = $entityTypeManager->getStorage('metatag_defaults');
}
+ /**
+ * Returns the list of protected defaults.
+ *
+ * @return array
+ * Th protected defaults.
+ */
+ public static function protectedDefaults() {
+ return [
+ 'global',
+ '403',
+ '404',
+ 'node',
+ 'front',
+ 'taxonomy_term',
+ 'user',
+ ];
+ }
+
/**
* {@inheritdoc}
*/
$fields = $this->getFields($entity);
- /* @var FieldConfig $field_info */
+ /* @var \Drupal\field\Entity\FieldConfig $field_info */
foreach ($fields as $field_name => $field_info) {
// Get the tags from this field.
$tags = $this->getFieldTags($entity, $field_name);
* {@inheritdoc}
*/
public function defaultTagsFromEntity(ContentEntityInterface $entity) {
- /** @var MetatagDefaults $metatags */
+ /** @var \Drupal\metatag\Entity\MetatagDefaults $metatags */
$metatags = $this->metatagDefaults->load('global');
if (!$metatags) {
return NULL;
if (!isset($groups[$tag_group])) {
// If the tag is claiming a group that has no matching plugin, log an
// error and force it to the basic group.
- $this->logger->error("Undefined group '%group' on tag '%tag'", ['%group' => $tag_group, '%tag' => $tag_id]);
+ $this->logger->error("Undefined group '%group' on tag '%tag'", ['%group' => $tag_group, '%tag' => $tag_name]);
$tag['group'] = 'basic';
$tag_group = 'basic';
}
}
/**
- * Returns a list of the metatags with values from a field.
+ * Returns a list of the meta tags with values from a field.
*
- * @param ContentEntityInterface $entity
+ * @param Drupal\Core\Entity\ContentEntityInterface $entity
+ * The ContentEntityInterface object.
* @param string $field_name
+ * The name of the field to work on.
*/
protected function getFieldTags(ContentEntityInterface $entity, $field_name) {
$tags = [];
/**
*
*
- * @param ContentEntityInterface $entity
+ * @param Drupal\Core\Entity\ContentEntityInterface $entity
+ * The entity to work on.
*/
public function getDefaultMetatags(ContentEntityInterface $entity = NULL) {
- // Get general global metatags
+ // Get general global metatags.
$metatags = $this->getGlobalMetatags();
// If that is empty something went wrong.
if (!$metatags) {
}
// Next check if there is this page is an entity that has meta tags.
- // @TODO: Think about using other defaults, e.g. views. Maybe use plugins?
+ // @todo Think about using other defaults, e.g. views. Maybe use plugins?
else {
if (is_null($entity)) {
$entity = metatag_get_route_entity();
}
if (!empty($entity)) {
- // Get default metatags for a given entity.
+ // Get default meta tags for a given entity.
$entity_defaults = $this->getEntityDefaultMetatags($entity);
if ($entity_defaults != NULL) {
$metatags->set('tags', array_merge($metatags->get('tags'), $entity_defaults));
/**
*
+ *
+ * @return array
+ * The global meta tags.
*/
public function getGlobalMetatags() {
return $this->metatagDefaults->load('global');
/**
*
+ *
+ * @return array
+ * The defaults for this page, if it's a special page.
*/
public function getSpecialMetatags() {
$metatags = NULL;
/**
*
+ *
+ * @param Drupal\Core\Entity\ContentEntityInterface $entity
+ * The entity to work with.
+ *
+ * @return array
+ * The appropriate default meta tags.
*/
public function getEntityDefaultMetatags(ContentEntityInterface $entity) {
$entity_metatags = $this->metatagDefaults->load($entity->getEntityTypeId());
}
/**
- * Generate the elements that go in the attached array in
- * hook_page_attachments.
+ * Generate the elements that go in the hook_page_attachments attached array.
*
* @param array $tags
* The array of tags as plugin_id => value.
* @return array
* Render array with tag elements.
*/
- public function generateElements($tags, $entity = NULL) {
+ public function generateElements(array $tags, $entity = NULL) {
$elements = [];
$tags = $this->generateRawElements($tags, $entity);
* @return array
* Render array with tag elements.
*/
- public function generateRawElements($tags, $entity = NULL) {
+ public function generateRawElements(array $tags, $entity = NULL) {
$rawTags = [];
$metatag_tags = $this->tagPluginManager->getDefinitions();
return ($weight_a < $weight_b) ? -1 : 1;
});
- // Each element of the $values array is a tag with the tag plugin name
- // as the key.
+ // Each element of the $values array is a tag with the tag plugin name as
+ // the key.
foreach ($tags as $tag_name => $value) {
// Check to ensure there is a matching plugin.
if (isset($metatag_tags[$tag_name])) {
// Render any tokens in the value.
$token_replacements = [];
if ($entity) {
- // @TODO: This needs a better way of discovering the context.
+ // @todo This needs a better way of discovering the context.
if ($entity instanceof ViewEntityInterface) {
// Views tokens require the ViewExecutable, not the config entity.
// @todo Can we move this into metatag_views somehow?
$token_replacements = ['view' => $entity->getExecutable()];
}
- else {
+ elseif ($entity instanceof ContentEntityInterface) {
$token_replacements = [$entity->getEntityTypeId() => $entity];
}
}
// Set the value as sometimes the data needs massaging, such as when
// field defaults are used for the Robots field, which come as an array
// that needs to be filtered and converted to a string.
- // @see @Robots::setValue().
+ // @see Robots::setValue()
$tag->setValue($value);
$langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
* Returns a list of fields handled by Metatag.
*
* @return array
+ * A list of supported field types.
*/
protected function fieldTypes() {
- //@TODO: Either get this dynamically from field plugins or forget it and just hardcode metatag where this is called.
+ // @todo Either get this dynamically from field plugins or forget it and
+ // just hardcode metatag where this is called.
return ['metatag'];
}