diff --git a/config/schema/metatag.metatag_tag.schema.yml b/config/schema/metatag.metatag_tag.schema.yml index 76b8202..7651a6c 100644 --- a/config/schema/metatag.metatag_tag.schema.yml +++ b/config/schema/metatag.metatag_tag.schema.yml @@ -36,11 +36,8 @@ metatag.metatag_tag.referrer: type: label label: 'Referrer policy' metatag.metatag_tag.robots: - type: sequence + type: label label: 'Robots' - sequence: - type: boolean - translatable: true metatag.metatag_tag.shortlink: type: label label: 'Shortlink' diff --git a/metatag.api.php b/metatag.api.php index 058a1c9..7f3c82c 100644 --- a/metatag.api.php +++ b/metatag.api.php @@ -30,10 +30,10 @@ function hook_metatag_route_entity(\Drupal\Core\Routing\RouteMatchInterface $rou * * @param array $metatags * The special metatags to be added to the page. - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity used for token replacements. + * @param array $context + * The context, containing the entity used for token replacements. */ -function hook_metatags_alter(array &$metatags, \Drupal\Core\Entity\EntityInterface $entity) { +function hook_metatags_alter(array &$metatags, array $context) { // Exclude metatags on frontpage. if (\Drupal::service('path.matcher')->isFrontPage()) { $metatags = NULL; diff --git a/metatag_views/src/MetatagViewsValuesCleanerTrait.php b/metatag_views/src/MetatagViewsValuesCleanerTrait.php index 4a1176a..a13f8a2 100644 --- a/metatag_views/src/MetatagViewsValuesCleanerTrait.php +++ b/metatag_views/src/MetatagViewsValuesCleanerTrait.php @@ -2,20 +2,21 @@ namespace Drupal\metatag_views; -use Drupal\views\ViewEntityInterface; - - +/** + * Collection of helper methods when handling raw tag values. + */ trait MetatagViewsValuesCleanerTrait { + /** * Clears the metatag form state values from illegal elements. * * @param array $metatags - * Array of values to submit. + * Array of values to submit. * * @return array - * Filtered metatag array. + * Filtered metatag array. */ - public function clearMetatagViewsDisallowedValues($metatags) { + public function clearMetatagViewsDisallowedValues(array $metatags) { // Get all legal tags. $tags = $this->metatagManager->sortedTags(); @@ -25,8 +26,11 @@ trait MetatagViewsValuesCleanerTrait { return $metatags; } + /** + * Removes tags that are empty. + */ public function removeEmptyTags($metatags) { - $metatags = array_filter($metatags, function($value) { + $metatags = array_filter($metatags, function ($value) { if (is_array($value)) { return count(array_filter($value)) > 0; } diff --git a/metatag_views/src/Plugin/views/display_extender/MetatagDisplayExtender.php b/metatag_views/src/Plugin/views/display_extender/MetatagDisplayExtender.php index deb6e5b..c9c1d11 100644 --- a/metatag_views/src/Plugin/views/display_extender/MetatagDisplayExtender.php +++ b/metatag_views/src/Plugin/views/display_extender/MetatagDisplayExtender.php @@ -1,17 +1,12 @@ metatagTagManager = $metatag_plugin_manager; + $this->metatagManager = $metatag_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('plugin.manager.metatag.tag'), + $container->get('metatag.manager') + ); + } + + /** * Provide a form to edit options for this plugin. */ public function buildOptionsForm(&$form, FormStateInterface $form_state) { @@ -40,7 +80,6 @@ class MetatagDisplayExtender extends DisplayExtenderPluginBase { $metatags = $this->getMetatags(); // Build/inject the Metatag form. - $this->metatagManager = \Drupal::service('metatag.manager'); $form['metatags'] = $this->metatagManager->form($metatags, $form, ['view']); } } @@ -56,10 +95,19 @@ class MetatagDisplayExtender extends DisplayExtenderPluginBase { */ public function submitOptionsForm(&$form, FormStateInterface $form_state) { if ($form_state->get('section') == 'metatags') { - $metatags = $form_state->getValues(); - // Remove the unnecessary elements from values array. - $metatags = $this->clearMetatagViewsDisallowedValues($metatags); - $this->options['metatags'] = $metatags; + // Process submitted metatag values and remove empty tags. + $tag_values = []; + $metatags = $form_state->cleanValues()->getValues(); + foreach ($metatags as $tag_id => $tag_value) { + // Some plugins need to process form input before storing it. + // Hence, we set it and then get it. + $tag = $this->metatagTagManager->createInstance($tag_id); + $tag->setValue($tag_value); + if (!empty($tag->value())) { + $tag_values[$tag_id] = $tag->value(); + } + } + $this->options['metatags'] = $tag_values; } } @@ -93,14 +141,16 @@ class MetatagDisplayExtender extends DisplayExtenderPluginBase { } /** - * Static member function to list which sections are defaultable - * and what items each section contains. + * Lists defaultable sections and items contained in each section. */ public function defaultableSections(&$sections, $section = NULL) { } /** * Identify whether or not the current display has custom meta tags defined. + * + * @return bool + * Whether or not the view has overridden metatags. */ protected function hasMetatags() { $metatags = $this->getMetatags(); @@ -124,7 +174,14 @@ class MetatagDisplayExtender extends DisplayExtenderPluginBase { return $metatags; } - public function setMetatags($metatags) { + /** + * Sets the metatags for the given view. + * + * @param array $metatags + * Metatag arrays as suitable for storage. + */ + public function setMetatags(array $metatags) { $this->options['metatags'] = $metatags; } + } diff --git a/src/Tests/MetatagAdminTest.php b/src/Tests/MetatagAdminTest.php index 9675ea6..dd3f661 100644 --- a/src/Tests/MetatagAdminTest.php +++ b/src/Tests/MetatagAdminTest.php @@ -121,7 +121,7 @@ class MetatagAdminTest extends WebTestBase { $robots_values = ['index', 'follow', 'noydir']; $values = []; foreach ($robots_values as $value) { - $values['robots[' . $value . ']'] = 1; + $values['robots[' . $value . ']'] = TRUE; } $this->drupalPostForm(NULL, $values, 'Save'); $this->assertText('Saved the Global Metatag defaults.'); diff --git a/src/Tests/MetatagTagsTest.php b/src/Tests/MetatagTagsTest.php index 7f036fb..52f6306 100644 --- a/src/Tests/MetatagTagsTest.php +++ b/src/Tests/MetatagTagsTest.php @@ -154,7 +154,7 @@ class MetatagTagsTest extends MetatagTagsTestBase { * Implements {meta_tag_name}_test_value() for 'robots'. */ public function robots_test_value() { - return 1; + return TRUE; } /**