View source
<?php
namespace Drupal\eva\Plugin\views\display;
use Drupal\Core\Entity\ContentEntityType;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Html;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\eva\ViewDisplays;
class Eva extends DisplayPluginBase {
protected $bundleInfo;
protected $entityTypeManager;
protected $currentPathStack;
protected $evaViewDisplays;
protected $usesAttachments = TRUE;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager, EntityTypeBundleInfoInterface $bundleInfo, CurrentPathStack $currentPathStack, ViewDisplays $evaViewDisplays) {
parent::__construct([], $plugin_id, $plugin_definition);
$this->entityTypeManager = $entityTypeManager;
$this->bundleInfo = $bundleInfo;
$this->currentPathStack = $currentPathStack;
$this->evaViewDisplays = $evaViewDisplays;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('entity_type.bundle.info'), $container
->get('path.current'), $container
->get('eva.view_displays'));
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['entity_type']['default'] = '';
$options['bundles']['default'] = [];
$options['argument_mode']['default'] = 'id';
$options['default_argument']['default'] = '';
$options['title']['default'] = '';
$options['defaults']['default']['title'] = FALSE;
return $options;
}
public function optionsSummary(&$categories, &$options) {
parent::optionsSummary($categories, $options);
$categories['entity_view'] = [
'title' => $this
->t('Entity content settings'),
'column' => 'second',
'build' => [
'#weight' => -10,
],
];
if ($entity_type = $this
->getOption('entity_type')) {
$entity_info = $this->entityTypeManager
->getDefinition($entity_type);
$type_name = $entity_info
->get('label');
$bundle_names = [];
$bundle_info = $this->bundleInfo
->getBundleInfo($entity_type);
foreach ($this
->getOption('bundles') as $bundle) {
$bundle_names[] = $bundle_info[$bundle]['label'];
}
}
$options['entity_type'] = [
'category' => 'entity_view',
'title' => $this
->t('Entity type'),
'value' => empty($type_name) ? $this
->t('None') : $type_name,
];
$options['bundles'] = [
'category' => 'entity_view',
'title' => $this
->t('Bundles'),
'value' => empty($bundle_names) ? $this
->t('All') : implode(', ', $bundle_names),
];
$argument_mode = $this
->getOption('argument_mode');
$options['arguments'] = [
'category' => 'entity_view',
'title' => $this
->t('Arguments'),
'value' => empty($argument_mode) ? $this
->t('None') : Html::escape($argument_mode),
];
$options['show_title'] = [
'category' => 'entity_view',
'title' => $this
->t('Show title'),
'value' => $this
->getOption('show_title') ? $this
->t('Yes') : $this
->t('No'),
];
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$entity_info = $this->entityTypeManager
->getDefinitions();
$entity_type = $this
->getOption('entity_type');
switch ($form_state
->get('section')) {
case 'entity_type':
foreach ($entity_info as $type => $info) {
if ($info instanceof ContentEntityType) {
$entity_names[$type] = $info
->get('label');
}
}
$form['#title'] .= $this
->t('Entity type');
$form['entity_type'] = [
'#type' => 'radios',
'#required' => TRUE,
'#validated' => TRUE,
'#title' => $this
->t('Attach this display to the following entity type'),
'#options' => $entity_names,
'#default_value' => $this
->getOption('entity_type'),
];
break;
case 'bundles':
$options = [];
foreach ($this->bundleInfo
->getBundleInfo($entity_type) as $bundle => $info) {
$options[$bundle] = $info['label'];
}
$form['#title'] .= $this
->t('Bundles');
$form['bundles'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Attach this display to the following bundles. If no bundles are selected, the display will be attached to all.'),
'#options' => $options,
'#default_value' => $this
->getOption('bundles'),
];
break;
case 'arguments':
$form['#title'] .= $this
->t('Arguments');
$default = $this
->getOption('argument_mode');
$options = [
'None' => $this
->t("No special handling"),
'id' => $this
->t("Use the ID of the entity the view is attached to"),
'token' => $this
->t("Use tokens from the entity the view is attached to"),
];
$form['argument_mode'] = [
'#type' => 'radios',
'#title' => $this
->t("How should this display populate the view's arguments?"),
'#options' => $options,
'#default_value' => $default,
];
$form['token'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Token replacement'),
'#collapsible' => TRUE,
'#states' => [
'visible' => [
':input[name=argument_mode]' => [
'value' => 'token',
],
],
],
];
$form['token']['default_argument'] = [
'#title' => $this
->t('Arguments'),
'#type' => 'textfield',
'#default_value' => $this
->getOption('default_argument'),
'#description' => $this
->t('You may use token replacement to provide arguments based on the current entity. Separate arguments with "/".'),
];
if (\Drupal::service('module_handler')
->moduleExists('token')) {
$token_types = [
$entity_type => $entity_type,
];
$token_mapper = \Drupal::service('token.entity_mapper');
if (!empty($token_types)) {
$token_types = array_map(function ($type) use ($token_mapper) {
return $token_mapper
->getTokenTypeForEntityType($type);
}, (array) $token_types);
}
$form['token']['browser'] = [
'#theme' => 'token_tree_link',
'#token_types' => $token_types,
'#global_types' => TRUE,
'#show_nested' => FALSE,
];
}
break;
case 'show_title':
$form['#title'] .= $this
->t('Show title');
$form['show_title'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show the title of the view above the attached view.'),
'#default_value' => $this
->getOption('show_title'),
];
break;
}
}
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
parent::validateOptionsForm($form, $form_state);
switch ($form_state
->get('section')) {
case 'entity_type':
if (empty($form_state
->getValue('entity_type'))) {
$form_state
->setError($form['entity_type'], $this
->t('Must select an entity'));
}
break;
}
}
public function validate() {
$errors = [];
if (empty($this
->getOption('entity_type'))) {
$errors[] = $this
->t('Display "@display" must be attached to an entity.', [
'@display' => $this->display['display_title'],
]);
}
return $errors;
}
public function remove() {
$longname = $this->view->storage
->get('id') . '_' . $this->display['id'];
$this->evaViewDisplays
->clearDetached($longname);
parent::remove();
}
public function submitOptionsForm(&$form, FormStateInterface $form_state) {
parent::submitOptionsForm($form, $form_state);
switch ($form_state
->get('section')) {
case 'entity_type':
$new_entity = $form_state
->getValue('entity_type');
$old_entity = $this
->getOption('entity_type');
$this
->setOption('entity_type', $new_entity);
if ($new_entity != $old_entity) {
$new_bundles_keys = $this->bundleInfo
->getBundleInfo($new_entity);
$new_bundles = [];
if (count($new_bundles_keys) == 1) {
$new_bundles[] = $new_bundles_keys[0];
}
$this
->setOption('bundles', $new_bundles);
}
break;
case 'bundles':
$this
->setOption('bundles', array_values(array_filter($form_state
->getValue('bundles'))));
break;
case 'arguments':
$this
->setOption('argument_mode', $form_state
->getValue('argument_mode'));
if ($form_state
->getValue('argument_mode') == 'token') {
$this
->setOption('default_argument', $form_state
->getValue('default_argument'));
}
else {
$this
->setOption('default_argument', NULL);
}
break;
case 'show_title':
$this
->setOption('show_title', $form_state
->getValue('show_title'));
break;
}
}
public function getPath() {
if (isset($this->view->current_entity)) {
$current_entity = $this->view->current_entity;
if ($current_entity
->hasLinkTemplate('canonical')) {
$uri = $current_entity
->toUrl('canonical');
if ($uri) {
$uri
->setAbsolute(TRUE);
return $uri
->toUriString();
}
}
}
return parent::getPath();
}
public function execute() {
if (!isset($this->view->override_path)) {
$this->view->override_path = $this->currentPathStack
->getPath();
}
$element = $this->view
->render();
if (!empty($this->view->result) || $this
->getOption('empty') || !empty($this->view->style_plugin->definition['even empty'])) {
return $element;
}
return [];
}
}