You are here

public function WorkflowsFieldItem::getSettableOptions in Workflows Field 8

Same name and namespace in other branches
  1. 2.x src/Plugin/Field/FieldType/WorkflowsFieldItem.php \Drupal\workflows_field\Plugin\Field\FieldType\WorkflowsFieldItem::getSettableOptions()

Returns an array of settable values with labels for display.

If the optional $account parameter is passed, then the array is filtered to values settable by the account.

Parameters

\Drupal\Core\Session\AccountInterface $account: (optional) The user account for which to filter the settable options. If omitted, all settable options are returned.

Return value

array An array of settable options for the object that may be used in an Options widget, usually when new data should be entered. It may either be a flat array of option labels keyed by values, or a two-dimensional array of option groups (array of flat option arrays, keyed by option group label). Note that labels should NOT be sanitized.

Overrides OptionsProviderInterface::getSettableOptions

1 call to WorkflowsFieldItem::getSettableOptions()
WorkflowsFieldItem::getSettableValues in src/Plugin/Field/FieldType/WorkflowsFieldItem.php
Returns an array of settable values.

File

src/Plugin/Field/FieldType/WorkflowsFieldItem.php, line 115

Class

WorkflowsFieldItem
Workflow state field item.

Namespace

Drupal\workflows_field\Plugin\Field\FieldType

Code

public function getSettableOptions(AccountInterface $account = NULL) {

  // $this->value is unpopulated due to https://www.drupal.org/node/2629932
  $field_name = $this
    ->getFieldDefinition()
    ->getName();
  $value = $this
    ->getEntity()
    ->get($field_name)->value;
  $workflow = $this
    ->getWorkflow();
  $type = $workflow
    ->getTypePlugin();
  $allowed_states = $type
    ->getStates();

  /** @var \Drupal\workflows\State $current */
  if ($value && $type
    ->hasState($value) && ($current = $type
    ->getState($value))) {
    $allowed_states = array_filter($allowed_states, function (StateInterface $state) use ($current, $workflow, $account) {
      if ($current
        ->id() === $state
        ->id()) {
        return TRUE;
      }

      // If we don't have a valid transition or we don't have an account then
      // all we care about is whether the transition is valid so return.
      $valid_transition = $current
        ->canTransitionTo($state
        ->id());
      if (!$valid_transition || !$account) {
        return $valid_transition;
      }

      // If we have an account object then ensure the user has permission to
      // this transition and that it's a valid transition.
      $transition = $current
        ->getTransitionTo($state
        ->id());
      return $account
        ->hasPermission(sprintf('use %s transition %s', $workflow
        ->id(), $transition
        ->id()));
    });
  }
  $state_labels = array_map(function ($state) {
    return $state
      ->label();
  }, $allowed_states);
  return $state_labels;
}