Section.php in Workbench Access 8
File
src/Plugin/views/field/Section.php
View source
<?php
namespace Drupal\workbench_access\Plugin\views\field;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\views\Plugin\views\field\MultiItemsFieldHandlerInterface;
use Drupal\views\Plugin\views\field\PrerenderList;
use Drupal\views\ResultRow;
use Drupal\workbench_access\Entity\AccessSchemeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Section extends PrerenderList implements MultiItemsFieldHandlerInterface {
protected $scheme;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
return $instance
->setScheme($container
->get('entity_type.manager')
->getStorage('access_scheme')
->load($configuration['scheme']));
}
public function setScheme(AccessSchemeInterface $scheme) {
$this->scheme = $scheme;
return $this;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['make_link'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Link to Section entity'),
'#default_value' => $this->options['make_link'],
];
return $form;
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['make_link'] = [
'default' => FALSE,
];
return $options;
}
public function query() {
$this
->ensureMyTable();
$this
->addAdditionalFields();
}
public function render_item($count, $item) {
return $item['value'];
}
public function getItems(ResultRow $values) {
$this->items = [];
if ($entity = $this
->getEntity($values)) {
$scheme = $this->scheme
->getAccessScheme();
$sections = $scheme
->getEntityValues($entity);
$tree = $scheme
->getTree();
foreach ($sections as $id) {
foreach ($tree as $root => $data) {
if (isset($data[$id])) {
if ($this->options['make_link'] && isset($data[$id]['path'])) {
$this->items[$id]['path'] = $data[$id]['path'];
$this->items[$id]['make_link'] = TRUE;
}
$this->items[$id]['value'] = $this
->sanitizeValue($data[$id]['label']);
}
}
}
}
return $this->items;
}
public function calculateDependencies() {
$dependencies = [];
$dependencies[$this->scheme
->getConfigDependencyKey()][] = $this->scheme
->getConfigDependencyName();
return $dependencies;
}
}
Classes
Name |
Description |
Section |
Field handler to present the section assigned to the node. |