View source
<?php
namespace Drupal\metatag_views\Plugin\views\display_extender;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\display_extender\DisplayExtenderPluginBase;
use Drupal\views\Plugin\views\style\StylePluginBase;
use Drupal\views\ViewExecutable;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class MetatagDisplayExtender extends DisplayExtenderPluginBase {
use StringTranslationTrait;
protected $metatagManager;
protected $metatagTagManager;
protected static $firstRowTokens;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->metatagTagManager = $container
->get('plugin.manager.metatag.tag');
$instance->metatagManager = $container
->get('metatag.manager');
return $instance;
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['metatags'] = [
'default' => [],
];
$options['tokenize'] = [
'default' => FALSE,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
if ($form_state
->get('section') == 'metatags') {
$form['#title'] .= $this
->t('The meta tags for this display');
$metatags = $this
->getMetatags(TRUE);
$form['metatags'] = $this->metatagManager
->form($metatags, $form, [
'view',
]);
$this
->tokenForm($form['metatags'], $form_state);
}
}
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
}
public function submitOptionsForm(&$form, FormStateInterface $form_state) {
if ($form_state
->get('section') == 'metatags') {
$tag_values = [];
$metatags = $form_state
->cleanValues()
->getValues();
$this->options['tokenize'] = $metatags['tokenize'] ?? FALSE;
unset($metatags['tokenize']);
foreach ($metatags as $tag_id => $tag_value) {
$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;
}
}
public function tokenForm(&$form, FormStateInterface $form_state) {
$form['tokenize'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use replacement tokens from the first row'),
'#default_value' => $this->options['tokenize'],
];
$options = [];
$optgroup_arguments = (string) t('Arguments');
$optgroup_fields = (string) t('Fields');
foreach ($this->view->display_handler
->getHandlers('field') as $field => $handler) {
$options[$optgroup_fields]["{{ {$field} }}"] = $handler
->adminLabel();
}
foreach ($this->view->display_handler
->getHandlers('argument') as $arg => $handler) {
$options[$optgroup_arguments]["{{ arguments.{$arg} }}"] = $this
->t('@argument title', [
'@argument' => $handler
->adminLabel(),
]);
$options[$optgroup_arguments]["{{ raw_arguments.{$arg} }}"] = $this
->t('@argument input', [
'@argument' => $handler
->adminLabel(),
]);
}
if (!empty($options)) {
$form['tokens'] = [
'#type' => 'details',
'#title' => $this
->t('Replacement patterns'),
'#open' => TRUE,
'#id' => 'edit-options-token-help',
'#states' => [
'visible' => [
':input[name="options[tokenize]"]' => [
'checked' => TRUE,
],
],
],
];
$form['tokens']['help'] = [
'#markup' => '<p>' . $this
->t('The following tokens are available. You may use Twig syntax in this field.') . '</p>',
];
foreach (array_keys($options) as $type) {
if (!empty($options[$type])) {
$items = [];
foreach ($options[$type] as $key => $value) {
$items[] = $key . ' == ' . $value;
}
$form['tokens'][$type]['tokens'] = [
'#theme' => 'item_list',
'#items' => $items,
];
}
}
}
$this
->globalTokenForm($form, $form_state);
}
public function preExecute() {
}
public function query() {
}
public function optionsSummary(&$categories, &$options) {
$categories['metatags'] = [
'title' => $this
->t('Meta tags'),
'column' => 'second',
];
$options['metatags'] = [
'category' => 'metatags',
'title' => $this
->t('Meta tags'),
'value' => $this
->hasMetatags() ? $this
->t('Overridden') : $this
->t('Using defaults'),
];
}
public function defaultableSections(&$sections, $section = NULL) {
}
protected function hasMetatags() {
$metatags = $this
->getMetatags();
return !empty($metatags);
}
public function getMetatags($raw = FALSE) {
$view = $this->view;
$metatags = [];
if (!empty($this->options['metatags'])) {
$metatags = $this->options['metatags'];
}
if ($this->options['tokenize'] && !$raw) {
if (self::$firstRowTokens) {
self::setFirstRowTokensOnStylePlugin($view, self::$firstRowTokens);
}
$style = $view
->getStyle();
foreach ($metatags as $key => $metatag) {
$metatag = $style
->tokenizeValue($metatag, 0);
$metatags[$key] = $this
->globalTokenReplace($metatag);
}
}
return $metatags;
}
public function setMetatags(array $metatags) {
$this->options['metatags'] = $metatags;
}
public function setFirstRowTokens(array $first_row_tokens) {
self::$firstRowTokens = $first_row_tokens;
}
public static function setFirstRowTokensOnStylePlugin(ViewExecutable $view, array $first_row_tokens) {
$style = $view
->getStyle();
self::getFirstRowTokensReflection($style)
->setValue($style, [
$first_row_tokens,
]);
}
public static function getFirstRowTokensFromStylePlugin(ViewExecutable $view) {
$style = $view
->getStyle();
return self::getFirstRowTokensReflection($style)
->getValue($style)[0] ?? [];
}
protected static function getFirstRowTokensReflection(StylePluginBase $style) : \ReflectionProperty {
$r = new \ReflectionObject($style);
$p = $r
->getProperty('rowTokens');
$p
->setAccessible(TRUE);
return $p;
}
}