3 namespace Drupal\metatag_views\Plugin\views\display_extender;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\metatag\MetatagManagerInterface;
7 use Drupal\metatag\MetatagTagPluginManager;
8 use Drupal\views\Plugin\views\display_extender\DisplayExtenderPluginBase;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
12 * Metatag display extender plugin.
14 * @ingroup views_display_extender_plugins
16 * @ViewsDisplayExtender(
17 * id = "metatag_display_extender",
18 * title = @Translation("Metatag display extender"),
19 * help = @Translation("Metatag settings for this view."),
23 class MetatagDisplayExtender extends DisplayExtenderPluginBase {
26 * The metatag manager.
28 * @var \Drupal\metatag\MetatagManagerInterface
30 protected $metatagManager;
33 * The plugin manager for metatag tags.
35 * @var \Drupal\metatag\MetatagTagPluginManager
37 protected $metatagTagManager;
40 * Constructs the plugin.
42 * @param array $configuration
43 * A configuration array containing information about the plugin instance.
44 * @param string $plugin_id
45 * The plugin_id for the plugin instance.
46 * @param mixed $plugin_definition
47 * The plugin implementation definition.
48 * @param \Drupal\metatag\MetatagTagPluginManager $metatag_plugin_manager
49 * The plugin manager for metatag tags.
50 * @param \Drupal\metatag\MetatagManagerInterface $metatag_manager
51 * The metatag manager.
53 public function __construct(array $configuration, $plugin_id, $plugin_definition, MetatagTagPluginManager $metatag_plugin_manager, MetatagManagerInterface $metatag_manager) {
54 parent::__construct($configuration, $plugin_id, $plugin_definition);
56 $this->metatagTagManager = $metatag_plugin_manager;
57 $this->metatagManager = $metatag_manager;
63 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
68 $container->get('plugin.manager.metatag.tag'),
69 $container->get('metatag.manager')
74 * Provide a form to edit options for this plugin.
76 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
78 if ($form_state->get('section') == 'metatags') {
79 $form['#title'] .= t('The meta tags for this display');
80 $metatags = $this->getMetatags();
82 // Build/inject the Metatag form.
83 $form['metatags'] = $this->metatagManager->form($metatags, $form, ['view']);
88 * Validate the options form.
90 public function validateOptionsForm(&$form, FormStateInterface $form_state) {
94 * Handle any special handling on the validate form.
96 public function submitOptionsForm(&$form, FormStateInterface $form_state) {
97 if ($form_state->get('section') == 'metatags') {
98 // Process submitted metatag values and remove empty tags.
100 $metatags = $form_state->cleanValues()->getValues();
101 foreach ($metatags as $tag_id => $tag_value) {
102 // Some plugins need to process form input before storing it.
103 // Hence, we set it and then get it.
104 $tag = $this->metatagTagManager->createInstance($tag_id);
105 $tag->setValue($tag_value);
106 if (!empty($tag->value())) {
107 $tag_values[$tag_id] = $tag->value();
110 $this->options['metatags'] = $tag_values;
115 * Set up any variables on the view prior to execution.
117 public function preExecute() {
121 * Inject anything into the query that the display_extender handler needs.
123 public function query() {
127 * Provide the default summary for options in the views UI.
129 * This output is returned as an array.
131 public function optionsSummary(&$categories, &$options) {
132 $categories['metatags'] = [
133 'title' => t('Meta tags'),
134 'column' => 'second',
136 $options['metatags'] = [
137 'category' => 'metatags',
138 'title' => t('Meta tags'),
139 'value' => $this->hasMetatags() ? t('Overridden') : t('Using defaults'),
144 * Lists defaultable sections and items contained in each section.
146 public function defaultableSections(&$sections, $section = NULL) {
150 * Identify whether or not the current display has custom meta tags defined.
153 * Whether or not the view has overridden metatags.
155 protected function hasMetatags() {
156 $metatags = $this->getMetatags();
157 return !empty($metatags);
162 * Get the Metatag configuration for this display.
165 * The meta tag values.
167 public function getMetatags() {
170 if (!empty($this->options['metatags'])) {
171 $metatags = $this->options['metatags'];
178 * Sets the meta tags for the given view.
180 * @param array $metatags
181 * Metatag arrays as suitable for storage.
183 public function setMetatags(array $metatags) {
184 $this->options['metatags'] = $metatags;