You are here

class WorkflowTransitionListBuilder in Workflow 8

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

Hierarchy

Expanded class hierarchy of WorkflowTransitionListBuilder

Deprecated

use View 'Workflow Entity history' in WorkflowTransitionListController.

See also

\Drupal\workflow\Entity\WorkflowState

File

src/WorkflowTransitionListBuilder.php, line 16

Namespace

Drupal\workflow
View source
class WorkflowTransitionListBuilder extends EntityListBuilder {
  const WORKFLOW_MARK_STATE_IS_DELETED = '*';

  /**
   * A variable to pass the entity of a transition to the ListBuilder.
   *
   * @var \Drupal\Core\Entity\EntityInterface
   */
  public $workflow_entity;

  /**
   * {@inheritdoc}
   */
  protected $limit = 50;

  /**
   * Indicates if a column 'Field name' must be generated.
   *
   * @var bool
   */
  protected $show_column_fieldname = NULL;

  /**
   * Indicates if a footer must be generated.
   *
   * @var bool
   */
  protected $footer_needed = FALSE;

  /**
   * {@inheritdoc}
   */
  public function load() {

    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    $entity = $this->workflow_entity;

    // N.B. This is a custom variable.
    $entity_type = $entity
      ->getEntityTypeId();
    $entity_id = $entity
      ->id();
    $field_name = workflow_url_get_field_name();

    // @todo D8: document $limit. Should be used in pager, not in load().
    // N.B. Using the provided default History view is recommended.
    $this->limit = \Drupal::config('workflow.settings')
      ->get('workflow_states_per_page');
    $limit = $this->limit;

    // Get Transitions with highest timestamp first.
    $entities = WorkflowTransition::loadMultipleByProperties($entity_type, [
      $entity_id,
    ], [], $field_name, '', $limit, 'DESC');
    return $entities;
  }

  /**
   * {@inheritdoc}
   *
   * Building the header and content lines for the contact list.
   *
   * Calling the parent::buildHeader() adds a column for the possible actions
   * and inserts the 'edit' and 'delete' links as defined for the entity type.
   */
  public function buildHeader() {
    $entity = $this->workflow_entity;

    // N.B. This is a custom variable.
    $header['timestamp'] = $this
      ->t('Date');
    if ($this
      ->showColumnFieldname($entity)) {
      $header['field_name'] = $this
        ->t('Field name');
    }
    $header['from_state'] = $this
      ->t('From State');
    $header['to_state'] = $this
      ->t('To State');
    $header['user_name'] = $this
      ->t('By');
    $header['comment'] = $this
      ->t('Comment');
    return $header + parent::buildHeader();
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $transition) {

    // Show the history table.

    /** @var \Drupal\workflow\Entity\WorkflowTransitionInterface $transition */
    $current_themed = FALSE;
    $entity = $transition
      ->getTargetEntity();
    $field_name = $transition
      ->getFieldName();
    $current_sid = workflow_node_current_state($entity, $field_name);
    $to_state = $transition
      ->getToState();
    if (!$to_state) {

      // This is an invalid/deleted state.
      $to_label = self::WORKFLOW_MARK_STATE_IS_DELETED;

      // Add a footer to explain the addition.
      $this->footer_needed = TRUE;
    }
    else {
      $label = Html::escape($this
        ->t($to_state
        ->label()));
      if ($transition
        ->getToSid() == $current_sid && $to_state
        ->isActive() && !$current_themed) {
        $to_label = $label;

        // Make a note that we have themed the current state; other times in the history
        // of this entity where the entity was in this state do not need to be specially themed.
        $current_themed = TRUE;
      }
      elseif (!$to_state
        ->isActive()) {
        $to_label = $label . self::WORKFLOW_MARK_STATE_IS_DELETED;

        // Add a footer to explain the addition.
        $this->footer_needed = TRUE;
      }
      else {

        // Regular state.
        $to_label = $label;
      }
    }
    unset($to_state);
    $from_state = $transition
      ->getFromState();
    if (!$from_state) {

      // This is an invalid/deleted state.
      $from_label = self::WORKFLOW_MARK_STATE_IS_DELETED;

      // Add a footer to explain the addition.
      $this->footer_needed = TRUE;
    }
    else {
      $label = Html::escape($this
        ->t($from_state
        ->label()));
      if (!$from_state
        ->isActive()) {
        $from_label = $label . self::WORKFLOW_MARK_STATE_IS_DELETED;

        // Add a footer to explain the addition.
        $this->footer_needed = TRUE;
      }
      else {

        // Regular state.
        $from_label = $label;
      }
    }
    unset($from_state);
    $owner = $transition
      ->getOwner();
    $field_label = $transition
      ->getFieldName();
    $variables = [
      'transition' => $transition,
      'extra' => '',
      'from_label' => $from_label,
      'to_label' => $to_label,
      'user' => $owner,
    ];

    // Allow other modules to modify the row.
    \Drupal::moduleHandler()
      ->alter('workflow_history', $variables);

    // 'class' => array('workflow_history_row'), // @todo D8-port.
    $row['timestamp']['data'] = $transition
      ->getTimestampFormatted();

    // 'class' => array('timestamp')
    // html_entity_decode() transforms chars like '&' correctly.
    if ($this
      ->showColumnFieldname($entity)) {
      $row['field_name']['data'] = html_entity_decode($field_label);
    }
    $row['from_state']['data'] = html_entity_decode($from_label);

    // 'class' => array('previous-state-name'))
    $row['to_state']['data'] = html_entity_decode($to_label);

    // 'class' => array('state-name'))
    $row['user_name']['data'] = $owner
      ->toLink($owner
      ->getDisplayName())
      ->toString();

    // 'class' => array('user-name')
    $row['comment']['data'] = html_entity_decode($transition
      ->getComment());

    // 'class' => array('log-comment')
    $row += parent::buildRow($transition);
    return $row;
  }

  /**
   * {@inheritdoc}
   *
   * Builds the entity listing as renderable array for table.html.twig.
   */
  public function render() {
    $build = [];

    // @todo D8: get pager working.
    $this->limit = \Drupal::config('workflow.settings')
      ->get('workflow_states_per_page');

    // @todo D8-port.
    // $output .= theme('pager', array('tags' => $limit)); // @todo D8-port.
    $build += parent::render();

    // Add a footer. This is not yet added in EntityListBuilder::render()
    if ($this->footer_needed) {

      // @todo D8-port: test this.
      // Two variants. First variant is official, but I like 2nd better.

      /*
            $build['table']['#footer'] = [
       [
         'class' => ['footer-class'],
         'data' => [
           [
             'data' => self::WORKFLOW_MARK_STATE_IS_DELETED . ' ' . $this->t('State is no longer available.'),
             'colspan' => count($build['table']['#header']),
           ],
         ],
       ],
            ];
      */
      $build['workflow_footer'] = [
        '#markup' => self::WORKFLOW_MARK_STATE_IS_DELETED . ' ' . $this
          ->t('State is no longer available.'),
        '#weight' => 500,
      ];
    }
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultOperations(EntityInterface $entity) {
    $operations = parent::getDefaultOperations($entity);
    if (isset($operations['edit'])) {
      $destination = \Drupal::destination()
        ->getAsArray();
      $operations['edit']['query'] = $destination;
    }
    return $operations;
  }

  /**
   * Gets the title of the page.
   *
   * @return string
   *   A string title of the page.
   */
  protected function getTitle() {
    return $this
      ->t('Workflow history');
  }

  /**
   * Determines if the column 'Field name' must be shown.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *
   * @return bool
   */
  protected function showColumnFieldname(EntityInterface $entity) {
    if (is_null($this->show_column_fieldname)) {

      // @todo Also remove when field_name is set in route??
      if (count(_workflow_info_fields($entity)) > 1) {
        $this->show_column_fieldname = TRUE;
      }
    }
    return $this->show_column_fieldname;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityHandlerBase::$moduleHandler protected property The module handler to invoke hooks on. 2
EntityHandlerBase::moduleHandler protected function Gets the module handler. 2
EntityHandlerBase::setModuleHandler public function Sets the module handler for this handler.
EntityListBuilder::$entityType protected property Information about the entity type.
EntityListBuilder::$entityTypeId protected property The entity type ID.
EntityListBuilder::$storage protected property The entity storage class. 1
EntityListBuilder::buildOperations public function Builds a renderable list of operation links for the entity. 2
EntityListBuilder::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityHandlerInterface::createInstance 20
EntityListBuilder::ensureDestination protected function Ensures that a destination is present on the given URL.
EntityListBuilder::getEntityIds protected function Loads entity IDs using a pager sorted by the entity id. 4
EntityListBuilder::getLabel Deprecated protected function Gets the label of an entity.
EntityListBuilder::getOperations public function Provides an array of information to build a list of operation links. Overrides EntityListBuilderInterface::getOperations 2
EntityListBuilder::getStorage public function Gets the entity storage. Overrides EntityListBuilderInterface::getStorage
EntityListBuilder::__construct public function Constructs a new EntityListBuilder object. 16
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
WorkflowTransitionListBuilder::$footer_needed protected property Indicates if a footer must be generated.
WorkflowTransitionListBuilder::$limit protected property The number of entities to list per page, or FALSE to list all entities. Overrides EntityListBuilder::$limit
WorkflowTransitionListBuilder::$show_column_fieldname protected property Indicates if a column 'Field name' must be generated.
WorkflowTransitionListBuilder::$workflow_entity public property A variable to pass the entity of a transition to the ListBuilder.
WorkflowTransitionListBuilder::buildHeader public function Building the header and content lines for the contact list. Overrides EntityListBuilder::buildHeader
WorkflowTransitionListBuilder::buildRow public function Builds a row for an entity in the entity listing. Overrides EntityListBuilder::buildRow
WorkflowTransitionListBuilder::getDefaultOperations public function Gets this list's default operations. Overrides EntityListBuilder::getDefaultOperations
WorkflowTransitionListBuilder::getTitle protected function Gets the title of the page. Overrides EntityListBuilder::getTitle
WorkflowTransitionListBuilder::load public function Loads entities of this type from storage for listing. Overrides EntityListBuilder::load
WorkflowTransitionListBuilder::render public function Builds the entity listing as renderable array for table.html.twig. Overrides EntityListBuilder::render
WorkflowTransitionListBuilder::showColumnFieldname protected function Determines if the column 'Field name' must be shown.
WorkflowTransitionListBuilder::WORKFLOW_MARK_STATE_IS_DELETED constant