You are here

public function WorkflowStateListBuilder::buildRow in Workflow 8

Builds a row for an entity in the entity listing.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity for this row of the list.

Return value

array A render array structure of fields for this entity.

Overrides DraggableListBuilder::buildRow

See also

\Drupal\Core\Entity\EntityListBuilder::render()

1 call to WorkflowStateListBuilder::buildRow()
WorkflowStateListBuilder::buildForm in src/WorkflowStateListBuilder.php
Form constructor.

File

src/WorkflowStateListBuilder.php, line 76

Class

WorkflowStateListBuilder
Defines a class to build a draggable listing of Workflow State entities.

Namespace

Drupal\workflow

Code

public function buildRow(EntityInterface $entity) {
  $row = [];
  if (!($workflow = workflow_url_get_workflow())) {
    return $row;
  }
  $wid = $url_wid = $workflow
    ->id();

  /** @var \Drupal\workflow\Entity\WorkflowState $entity */
  $state = $entity;
  $sid = $state
    ->id();
  $label = $state
    ->label();
  $count = $state
    ->count();

  // Build select options for reassigning states.
  // We put a blank state first for validation.
  $state_options = [
    '' => ' ',
  ];
  $state_options += workflow_get_workflow_state_names($wid, FALSE);

  // Make it impossible to reassign to the same state that is disabled.
  $current_state_options = [];
  if ($state
    ->isActive() && !$state
    ->isCreationState() && $sid) {
    $current_state = [
      $sid => $state_options[$sid],
    ];
    $current_state_options = array_diff($state_options, $current_state);
  }

  /*
   *  Build the Row.
   */

  // The column 'weight' is added by parent in the draggable EntityList.
  //  $row['weight'] = $state->weight;
  // Some columns are not welcome in the list.
  //  $row['module'] = $state->getModule();
  //  $row['wid'] = $state->getWorkflow();
  // Add separate empty column for Drag handle for UX reasons.
  // Drag handle can be invisible at the end of this function.
  $row['drag_handle'] = [
    '#type' => 'label',
  ];

  // Column 'label' is manipulated in parent::buildForm(). Use 'label_new'.
  $row['label_new'] = [
    '#markup' => $label,
    '#type' => 'textfield',
    '#size' => 30,
    '#maxlength' => 255,
    '#default_value' => $label,
    '#title' => NULL,
    // This hides the red 'required' asterisk.
    '#disabled' => !$state
      ->isActive(),
  ];
  $row['id'] = [
    '#type' => 'machine_name',
    '#title' => NULL,
    // This hides the red 'required' asterisk.
    '#size' => 30,
    '#description' => NULL,
    '#disabled' => TRUE,
    '#default_value' => $state
      ->id(),
    // N.B.: Keep machine_name in WorkflowState and ~ListBuilder aligned.
    '#required' => FALSE,
    // @todo D8: enable machine_name as interactive WorkflowState element.
    '#machine_name' => [
      // Add local helper function 'exists' at the bottom of this class.
      'exists' => [
        $this,
        'exists',
      ],
      // 'source' => ['label_new'],
      'source' => [
        'states',
        $state
          ->id(),
        'label_new',
      ],
      // 'replace_pattern' =>'([^a-z0-9_]+)|(^custom$)',
      // Add '()' characters from exclusion list since creation state has it.
      'replace_pattern' => '[^a-z0-9_()]+',
      // Add '()' characters from exclusion list since creation state has it.
      'error' => $this
        ->t('The machine-readable name must be unique, and can only contain lowercase letters, numbers, and underscores.'),
    ],
  ];
  $row['sysid'] = [
    '#type' => 'value',
    '#value' => $state->sysid,
  ];
  $row['status'] = [
    '#type' => 'checkbox',
    '#default_value' => $state
      ->isActive(),
    '#disabled' => $state
      ->isCreationState() || !$sid,
  ];

  // The new value of states that are inactivated.
  $row['reassign'] = [
    '#type' => 'select',
    '#options' => $current_state_options,
  ];
  $row['count'] = [
    '#type' => 'value',
    '#value' => $count,
    '#markup' => $count,
  ];
  $row += parent::buildRow($entity);
  if ($state
    ->isCreationState()) {

    // Hide Drag handle for Creation state. It is always first.
    $row['#attributes']['class'] = array_diff($row['#attributes']['class'], [
      'draggable',
    ]);
  }
  if (!$state
    ->isActive() || $state
    ->isCreationState() || !$sid || !$count) {

    // New state and disabled states cannot be reassigned.
    $row['reassign']['#type'] = 'hidden';
    $row['reassign']['#disabled'] = TRUE;
  }
  return $row;
}