You are here

protected function StatesListFormatter::buildItems in Workflows Field 8

Same name and namespace in other branches
  1. 2.x src/Plugin/Field/FieldFormatter/StatesListFormatter.php \Drupal\workflows_field\Plugin\Field\FieldFormatter\StatesListFormatter::buildItems()

Builds the items array for theme item list.

Parameters

\Drupal\workflows_field\Plugin\Field\FieldType\WorkflowsFieldItem $item: The currently active workflow item.

Return value

array An array of items for theme item_list.

1 call to StatesListFormatter::buildItems()
StatesListFormatter::viewElements in src/Plugin/Field/FieldFormatter/StatesListFormatter.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/StatesListFormatter.php, line 51

Class

StatesListFormatter
Plugin implementation of the 'workflows_field_state_list' formatter.

Namespace

Drupal\workflows_field\Plugin\Field\FieldFormatter

Code

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;

    // Once we've found the current item no longer mark the items as before
    // current. We only apply sibling classes when the item is not the current
    // item.
    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;
}