You are here

public function WorkflowStateHistoryFormatter::viewElements in Workflow 8

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/WorkflowStateHistoryFormatter.php, line 110

Class

WorkflowStateHistoryFormatter
Provides a workflow state history formatter.

Namespace

Drupal\workflow\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $output = [];
  $field_name = $this->fieldDefinition
    ->getName();
  $entity = $items
    ->getEntity();
  $entity_type = $entity
    ->getEntityTypeId();

  // @todo Expose limit into formatter settings form.
  $limit = 10;
  $workflowTransitions = WorkflowTransition::loadMultipleByProperties($entity_type, [
    $entity
      ->id(),
  ], [], $field_name, '', $limit, 'DESC');
  if ($workflowTransitions) {
    $workflowTransitionsViews = [];
    foreach ($workflowTransitions as $workflowTransition) {
      $workflowTransitionsView = $this->viewBuilder
        ->view($workflowTransition, 'default');
      $workflowTransitionsViews[] = $workflowTransitionsView;
    }
    $output[] = [
      '#theme' => 'item_list',
      '#items' => $workflowTransitionsViews,
    ];
  }
  else {
    $output[] = [
      '#markup' => Markup::create($this
        ->t('Empty')),
    ];
  }
  return $output;
}