View source
<?php
namespace Drupal\workflows_field\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\workflows\Entity\Workflow;
use Drupal\workflows\StateInterface;
use Drupal\workflows_field\Plugin\Field\FieldType\WorkflowsFieldItem;
class StatesListFormatter extends FormatterBase {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$elements[$delta] = [
'#theme' => 'item_list__states_list',
'#context' => [
'list_style' => 'workflows-states-list',
],
'#attributes' => [
'class' => [
Html::cleanCssIdentifier($item->value) . '--active',
],
],
'#items' => $this
->buildItems($item),
];
}
return $elements;
}
protected function buildItems(WorkflowsFieldItem $item) {
$excluded = array_filter($this
->getSetting('excluded_states'));
$items = [];
$before_current = TRUE;
foreach ($this
->getStatesFromWorkflow() as $key => $state) {
$is_current = $item->value === $key;
if ($is_current) {
$before_current = FALSE;
$class = 'is-current';
}
else {
$class = $before_current ? 'before-current' : 'after-current';
}
if (!in_array($key, $excluded, TRUE)) {
$items[] = [
'#type' => 'html_tag',
'#tag' => 'span',
'#value' => $state
->label(),
'#wrapper_attributes' => [
'class' => [
$key,
$class,
],
],
];
}
}
return $items;
}
public static function defaultSettings() {
return [
'excluded_states' => [],
];
}
protected function getStatesFromWorkflow() {
$workflow = Workflow::load($this
->getFieldSetting('workflow'));
$type = $workflow
->getTypePlugin();
return $type
->getStates();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$elements['excluded_states'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Excluded states'),
'#options' => array_map(function (StateInterface $state) {
return $state
->label();
}, $this
->getStatesFromWorkflow()),
'#default_value' => $this
->getSetting('excluded_states'),
];
return $elements;
}
public function settingsSummary() {
$summary = [];
if ($excluded = array_filter($this
->getSetting('excluded_states'))) {
$summary[] = $this
->t('Excluded states: @states', [
'@states' => implode(', ', $excluded),
]);
}
else {
$summary[] = $this
->t('Excluded states: n/a');
}
return $summary;
}
}