3 namespace Drupal\metatag;
5 use Drupal\Component\Render\PlainTextOutput;
6 use Drupal\Core\Entity\ContentEntityInterface;
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
8 use Drupal\Core\Language\LanguageInterface;
9 use Drupal\Core\Logger\LoggerChannelFactoryInterface;
10 use Drupal\metatag\Entity\MetatagDefaults;
11 use Drupal\views\ViewEntityInterface;
14 * Class MetatagManager.
16 * @package Drupal\metatag
18 class MetatagManager implements MetatagManagerInterface {
21 * @var \Drupal\metatag\MetatagGroupPluginManager
23 protected $groupPluginManager;
26 * @var \Drupal\metatag\MetatagTagPluginManager
28 protected $tagPluginManager;
33 protected $metatagDefaults;
36 * @var \Drupal\metatag\MetatagToken
38 protected $tokenService;
41 * Metatag logging channel.
43 * @var \Drupal\Core\Logger\LoggerChannelInterface
48 * Constructor for MetatagManager.
50 * @param Drupal\metatag\MetatagGroupPluginManager $groupPluginManager
51 * The MetatagGroupPluginManager object.
52 * @param Drupal\metatag\MetatagTagPluginManager $tagPluginManager
53 * The MetatagTagPluginMÏ€anager object.
54 * @param Drupal\metatag\MetatagToken $token
55 * The MetatagToken object.
56 * @param Drupal\Core\Logger\LoggerChannelFactoryInterface $channelFactory
57 * The LoggerChannelFactoryInterface object.
58 * @param Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
59 * The EntityTypeManagerInterface object.
61 public function __construct(MetatagGroupPluginManager $groupPluginManager,
62 MetatagTagPluginManager $tagPluginManager,
64 LoggerChannelFactoryInterface $channelFactory,
65 EntityTypeManagerInterface $entityTypeManager) {
66 $this->groupPluginManager = $groupPluginManager;
67 $this->tagPluginManager = $tagPluginManager;
68 $this->tokenService = $token;
69 $this->logger = $channelFactory->get('metatag');
70 $this->metatagDefaults = $entityTypeManager->getStorage('metatag_defaults');
74 * Returns the list of protected defaults.
77 * Th protected defaults.
79 public static function protectedDefaults() {
94 public function tagsFromEntity(ContentEntityInterface $entity) {
97 $fields = $this->getFields($entity);
99 /* @var \Drupal\field\Entity\FieldConfig $field_info */
100 foreach ($fields as $field_name => $field_info) {
101 // Get the tags from this field.
102 $tags = $this->getFieldTags($entity, $field_name);
111 public function tagsFromEntityWithDefaults(ContentEntityInterface $entity) {
112 return $this->tagsFromEntity($entity) + $this->defaultTagsFromEntity($entity);
118 public function defaultTagsFromEntity(ContentEntityInterface $entity) {
119 /** @var \Drupal\metatag\Entity\MetatagDefaults $metatags */
120 $metatags = $this->metatagDefaults->load('global');
124 // Add/overwrite with tags set on the entity type.
125 $entity_type_tags = $this->metatagDefaults->load($entity->getEntityTypeId());
126 if (!is_null($entity_type_tags)) {
127 $metatags->overwriteTags($entity_type_tags->get('tags'));
129 // Add/overwrite with tags set on the entity bundle.
130 $bundle_metatags = $this->metatagDefaults->load($entity->getEntityTypeId() . '__' . $entity->bundle());
131 if (!is_null($bundle_metatags)) {
132 $metatags->overwriteTags($bundle_metatags->get('tags'));
134 return $metatags->get('tags');
138 * Gets the group plugin definitions.
143 protected function groupDefinitions() {
144 return $this->groupPluginManager->getDefinitions();
148 * Gets the tag plugin definitions.
153 protected function tagDefinitions() {
154 return $this->tagPluginManager->getDefinitions();
160 public function sortedGroups() {
161 $metatag_groups = $this->groupDefinitions();
163 // Pull the data from the definitions into a new array.
165 foreach ($metatag_groups as $group_name => $group_info) {
166 $groups[$group_name]['id'] = $group_info['id'];
167 $groups[$group_name]['label'] = $group_info['label']->render();
168 $groups[$group_name]['description'] = $group_info['description'];
169 $groups[$group_name]['weight'] = $group_info['weight'];
172 // Create the 'sort by' array.
174 foreach ($groups as $group) {
175 $sort_by[] = $group['weight'];
178 // Sort the groups by weight.
179 array_multisort($sort_by, SORT_ASC, $groups);
187 public function sortedTags() {
188 $metatag_tags = $this->tagDefinitions();
190 // Pull the data from the definitions into a new array.
192 foreach ($metatag_tags as $tag_name => $tag_info) {
193 $tags[$tag_name]['id'] = $tag_info['id'];
194 $tags[$tag_name]['label'] = $tag_info['label']->render();
195 $tags[$tag_name]['group'] = $tag_info['group'];
196 $tags[$tag_name]['weight'] = $tag_info['weight'];
199 // Create the 'sort by' array.
201 foreach ($tags as $key => $tag) {
202 $sort_by['group'][$key] = $tag['group'];
203 $sort_by['weight'][$key] = $tag['weight'];
206 // Sort the tags by weight.
207 array_multisort($sort_by['group'], SORT_ASC, $sort_by['weight'], SORT_ASC, $tags);
215 public function sortedGroupsWithTags() {
216 $groups = $this->sortedGroups();
217 $tags = $this->sortedTags();
219 foreach ($tags as $tag_name => $tag) {
220 $tag_group = $tag['group'];
222 if (!isset($groups[$tag_group])) {
223 // If the tag is claiming a group that has no matching plugin, log an
224 // error and force it to the basic group.
225 $this->logger->error("Undefined group '%group' on tag '%tag'", ['%group' => $tag_group, '%tag' => $tag_name]);
226 $tag['group'] = 'basic';
227 $tag_group = 'basic';
230 $groups[$tag_group]['tags'][$tag_name] = $tag;
239 public function form(array $values, array $element, array $token_types = [], array $included_groups = NULL, array $included_tags = NULL) {
240 // Add the outer fieldset.
242 '#type' => 'details',
245 $element += $this->tokenService->tokenBrowser($token_types);
247 $groups_and_tags = $this->sortedGroupsWithTags();
250 foreach ($groups_and_tags as $group_name => $group) {
251 // Only act on groups that have tags and are in the list of included
252 // groups (unless that list is null).
253 if (isset($group['tags']) && (is_null($included_groups) || in_array($group_name, $included_groups) || in_array($group['id'], $included_groups))) {
254 // Create the fieldset.
255 $element[$group_name]['#type'] = 'details';
256 $element[$group_name]['#title'] = $group['label'];
257 $element[$group_name]['#description'] = $group['description'];
258 $element[$group_name]['#open'] = $first;
261 foreach ($group['tags'] as $tag_name => $tag) {
262 // Only act on tags in the included tags list, unless that is null.
263 if (is_null($included_tags) || in_array($tag_name, $included_tags) || in_array($tag['id'], $included_tags)) {
264 // Make an instance of the tag.
265 $tag = $this->tagPluginManager->createInstance($tag_name);
267 // Set the value to the stored value, if any.
268 $tag_value = isset($values[$tag_name]) ? $values[$tag_name] : NULL;
269 $tag->setValue($tag_value);
271 // Create the bit of form for this tag.
272 $element[$group_name][$tag_name] = $tag->form($element);
282 * Returns a list of the metatag fields on an entity.
284 protected function getFields(ContentEntityInterface $entity) {
287 if ($entity instanceof ContentEntityInterface) {
288 // Get a list of the metatag field types.
289 $field_types = $this->fieldTypes();
291 // Get a list of the field definitions on this entity.
292 $definitions = $entity->getFieldDefinitions();
294 // Iterate through all the fields looking for ones in our list.
295 foreach ($definitions as $field_name => $definition) {
296 // Get the field type, ie: metatag.
297 $field_type = $definition->getType();
299 // Check the field type against our list of fields.
300 if (isset($field_type) && in_array($field_type, $field_types)) {
301 $field_list[$field_name] = $definition;
310 * Returns a list of the meta tags with values from a field.
312 * @param Drupal\Core\Entity\ContentEntityInterface $entity
313 * The ContentEntityInterface object.
314 * @param string $field_name
315 * The name of the field to work on.
317 protected function getFieldTags(ContentEntityInterface $entity, $field_name) {
319 foreach ($entity->{$field_name} as $item) {
320 // Get serialized value and break it into an array of tags with values.
321 $serialized_value = $item->get('value')->getValue();
322 if (!empty($serialized_value)) {
323 $tags += unserialize($serialized_value);
333 * @param Drupal\Core\Entity\ContentEntityInterface $entity
334 * The entity to work on.
336 public function getDefaultMetatags(ContentEntityInterface $entity = NULL) {
337 // Get general global metatags.
338 $metatags = $this->getGlobalMetatags();
339 // If that is empty something went wrong.
344 // Check if this is a special page.
345 $special_metatags = $this->getSpecialMetatags();
347 // Merge with all globals defaults.
348 if ($special_metatags) {
349 $metatags->set('tags', array_merge($metatags->get('tags'), $special_metatags->get('tags')));
352 // Next check if there is this page is an entity that has meta tags.
353 // @todo Think about using other defaults, e.g. views. Maybe use plugins?
355 if (is_null($entity)) {
356 $entity = metatag_get_route_entity();
359 if (!empty($entity)) {
360 // Get default meta tags for a given entity.
361 $entity_defaults = $this->getEntityDefaultMetatags($entity);
362 if ($entity_defaults != NULL) {
363 $metatags->set('tags', array_merge($metatags->get('tags'), $entity_defaults));
368 return $metatags->get('tags');
375 * The global meta tags.
377 public function getGlobalMetatags() {
378 return $this->metatagDefaults->load('global');
385 * The defaults for this page, if it's a special page.
387 public function getSpecialMetatags() {
390 if (\Drupal::service('path.matcher')->isFrontPage()) {
391 $metatags = $this->metatagDefaults->load('front');
393 elseif (\Drupal::service('current_route_match')->getRouteName() == 'system.403') {
394 $metatags = $this->metatagDefaults->load('403');
396 elseif (\Drupal::service('current_route_match')->getRouteName() == 'system.404') {
397 $metatags = $this->metatagDefaults->load('404');
406 * @param Drupal\Core\Entity\ContentEntityInterface $entity
407 * The entity to work with.
410 * The appropriate default meta tags.
412 public function getEntityDefaultMetatags(ContentEntityInterface $entity) {
413 $entity_metatags = $this->metatagDefaults->load($entity->getEntityTypeId());
415 if ($entity_metatags != NULL) {
416 // Merge with global defaults.
417 $metatags = array_merge($metatags, $entity_metatags->get('tags'));
420 // Finally, check if we should apply bundle overrides.
421 $bundle_metatags = $this->metatagDefaults->load($entity->getEntityTypeId() . '__' . $entity->bundle());
422 if ($bundle_metatags != NULL) {
423 // Merge with existing defaults.
424 $metatags = array_merge($metatags, $bundle_metatags->get('tags'));
431 * Generate the elements that go in the hook_page_attachments attached array.
434 * The array of tags as plugin_id => value.
435 * @param object $entity
436 * Optional entity object to use for token replacements.
439 * Render array with tag elements.
441 public function generateElements(array $tags, $entity = NULL) {
443 $tags = $this->generateRawElements($tags, $entity);
445 foreach ($tags as $name => $tag) {
447 $elements['#attached']['html_head'][] = [
458 * Generate the actual meta tag values.
461 * The array of tags as plugin_id => value.
462 * @param object $entity
463 * Optional entity object to use for token replacements.
466 * Render array with tag elements.
468 public function generateRawElements(array $tags, $entity = NULL) {
471 $metatag_tags = $this->tagPluginManager->getDefinitions();
473 // Order the elements by weight first, as some systems like Facebook care.
474 uksort($tags, function ($tag_name_a, $tag_name_b) use ($metatag_tags) {
475 $weight_a = isset($metatag_tags[$tag_name_a]['weight']) ? $metatag_tags[$tag_name_a]['weight'] : 0;
476 $weight_b = isset($metatag_tags[$tag_name_b]['weight']) ? $metatag_tags[$tag_name_b]['weight'] : 0;
478 return ($weight_a < $weight_b) ? -1 : 1;
481 // Each element of the $values array is a tag with the tag plugin name as
483 foreach ($tags as $tag_name => $value) {
484 // Check to ensure there is a matching plugin.
485 if (isset($metatag_tags[$tag_name])) {
486 // Get an instance of the plugin.
487 $tag = $this->tagPluginManager->createInstance($tag_name);
489 // Render any tokens in the value.
490 $token_replacements = [];
492 // @todo This needs a better way of discovering the context.
493 if ($entity instanceof ViewEntityInterface) {
494 // Views tokens require the ViewExecutable, not the config entity.
495 // @todo Can we move this into metatag_views somehow?
496 $token_replacements = ['view' => $entity->getExecutable()];
498 elseif ($entity instanceof ContentEntityInterface) {
499 $token_replacements = [$entity->getEntityTypeId() => $entity];
503 // Set the value as sometimes the data needs massaging, such as when
504 // field defaults are used for the Robots field, which come as an array
505 // that needs to be filtered and converted to a string.
506 // @see Robots::setValue()
507 $tag->setValue($value);
508 $langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
510 if ($tag->type() === 'image') {
511 $processed_value = $this->tokenService->replace($tag->value(), $token_replacements, ['langcode' => $langcode]);
514 $processed_value = PlainTextOutput::renderFromHtml(htmlspecialchars_decode($this->tokenService->replace($tag->value(), $token_replacements, ['langcode' => $langcode])));
517 // Now store the value with processed tokens back into the plugin.
518 $tag->setValue($processed_value);
520 // Have the tag generate the output based on the value we gave it.
521 $output = $tag->output();
523 if (!empty($output)) {
524 $rawTags[$tag_name] = $output;
533 * Returns a list of fields handled by Metatag.
536 * A list of supported field types.
538 protected function fieldTypes() {
539 // @todo Either get this dynamically from field plugins or forget it and
540 // just hardcode metatag where this is called.