use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
-use Drupal\metatag\Entity\MetatagDefaults;
use Drupal\views\ViewEntityInterface;
/**
class MetatagManager implements MetatagManagerInterface {
/**
+ * The group plugin manager.
+ *
* @var \Drupal\metatag\MetatagGroupPluginManager
*/
protected $groupPluginManager;
/**
+ * The tag plugin manager.
+ *
* @var \Drupal\metatag\MetatagTagPluginManager
*/
protected $tagPluginManager;
/**
+ * The Metatag defaults.
+ *
* @var array
*/
protected $metatagDefaults;
/**
+ * The Metatag token.
+ *
* @var \Drupal\metatag\MetatagToken
*/
protected $tokenService;
/**
- * Metatag logging channel.
+ * The Metatag logging channel.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
/**
* Constructor for MetatagManager.
*
- * @param Drupal\metatag\MetatagGroupPluginManager $groupPluginManager
+ * @param \Drupal\metatag\MetatagGroupPluginManager $groupPluginManager
* The MetatagGroupPluginManager object.
- * @param Drupal\metatag\MetatagTagPluginManager $tagPluginManager
+ * @param \Drupal\metatag\MetatagTagPluginManager $tagPluginManager
* The MetatagTagPluginMÏ€anager object.
- * @param Drupal\metatag\MetatagToken $token
+ * @param \Drupal\metatag\MetatagToken $token
* The MetatagToken object.
- * @param Drupal\Core\Logger\LoggerChannelFactoryInterface $channelFactory
+ * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $channelFactory
* The LoggerChannelFactoryInterface object.
- * @param Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
+ * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The EntityTypeManagerInterface object.
*/
public function __construct(MetatagGroupPluginManager $groupPluginManager,
$groups_and_tags = $this->sortedGroupsWithTags();
- $first = TRUE;
foreach ($groups_and_tags as $group_name => $group) {
// Only act on groups that have tags and are in the list of included
// groups (unless that list is null).
$element[$group_name]['#type'] = 'details';
$element[$group_name]['#title'] = $group['label'];
$element[$group_name]['#description'] = $group['description'];
- $element[$group_name]['#open'] = $first;
- $first = FALSE;
+ $element[$group_name]['#open'] = FALSE;
foreach ($group['tags'] as $tag_name => $tag) {
// Only act on tags in the included tags list, unless that is null.
$tag_value = isset($values[$tag_name]) ? $values[$tag_name] : NULL;
$tag->setValue($tag_value);
+ // Open any groups that have non-empty values.
+ if (!empty($tag_value)) {
+ $element[$group_name]['#open'] = TRUE;
+ }
+
// Create the bit of form for this tag.
$element[$group_name][$tag_name] = $tag->form($element);
}
}
/**
- * Returns a list of the metatag fields on an entity.
+ * Returns a list of the Metatag fields on an entity.
+ *
+ * @param \Drupal\Core\Entity\ContentEntityInterface $entity
+ * The entity to examine.
+ *
+ * @return array
+ * The fields from the entity which are Metatag fields.
*/
protected function getFields(ContentEntityInterface $entity) {
$field_list = [];
/**
* Returns a list of the meta tags with values from a field.
*
- * @param Drupal\Core\Entity\ContentEntityInterface $entity
+ * @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The ContentEntityInterface object.
* @param string $field_name
* The name of the field to work on.
+ *
+ * @return array
+ * Array of field tags.
*/
protected function getFieldTags(ContentEntityInterface $entity, $field_name) {
$tags = [];
}
/**
+ * Returns default meta tags for an entity.
*
- *
- * @param Drupal\Core\Entity\ContentEntityInterface $entity
+ * @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity to work on.
+ *
+ * @return array
+ * The default meta tags appropriate for this entity.
*/
public function getDefaultMetatags(ContentEntityInterface $entity = NULL) {
// Get general global metatags.
}
/**
- *
+ * Returns global meta tags.
*
* @return array
* The global meta tags.
}
/**
- *
+ * Returns special meta tags.
*
* @return array
* The defaults for this page, if it's a special page.
}
/**
+ * Returns default meta tags for an entity.
*
- *
- * @param Drupal\Core\Entity\ContentEntityInterface $entity
+ * @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity to work with.
*
* @return array
* Render array with tag elements.
*/
public function generateRawElements(array $tags, $entity = NULL) {
+ // Ignore the update.php path.
+ $request = \Drupal::request();
+ if ($request->getBaseUrl() == '/update.php') {
+ return [];
+ }
+
$rawTags = [];
$metatag_tags = $this->tagPluginManager->getDefinitions();
$tag->setValue($value);
$langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
- if ($tag->type() === 'image') {
- $processed_value = $this->tokenService->replace($tag->value(), $token_replacements, ['langcode' => $langcode]);
- }
- else {
- $processed_value = PlainTextOutput::renderFromHtml(htmlspecialchars_decode($this->tokenService->replace($tag->value(), $token_replacements, ['langcode' => $langcode])));
- }
+ $processed_value = PlainTextOutput::renderFromHtml(htmlspecialchars_decode($this->tokenService->replace($tag->value(), $token_replacements, ['langcode' => $langcode])));
// Now store the value with processed tokens back into the plugin.
$tag->setValue($processed_value);
$output = $tag->output();
if (!empty($output)) {
- $rawTags[$tag_name] = $output;
+ $output = $tag->multiple() ? $output : [$output];
+
+ // Backwards compatibility for modules which don't support this logic.
+ if (isset($output['#tag'])) {
+ $output = [$output];
+ }
+
+ foreach ($output as $index => $element) {
+ // Add index to tag name as suffix to avoid having same key.
+ $index_tag_name = $tag->multiple() ? $tag_name . '_' . $index : $tag_name;
+ $rawTags[$index_tag_name] = $element;
+ }
}
}
}