You are here

class ReplicationListBuilder in Deploy - Content Staging 8

Defines a class to build a listing of Replication entities.

Hierarchy

Expanded class hierarchy of ReplicationListBuilder

File

src/ReplicationListBuilder.php, line 20

Namespace

Drupal\deploy
View source
class ReplicationListBuilder extends EntityListBuilder {

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header['replication_status'] = $this
      ->t('Status');
    $header['name'] = $this
      ->t('Title');
    $header['source'] = $this
      ->t('Source');
    $header['target'] = $this
      ->t('Target');
    $header['changed'] = $this
      ->t('Updated');
    $header['created'] = $this
      ->t('Created');
    $header['user'] = $this
      ->t('User');
    $header['description'] = $this
      ->t('Description');
    $header['operations'] = $this
      ->t('Operations');
    return $header;
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {
    $formatter = \Drupal::service('date.formatter');

    /* @var $entity \Drupal\workspace\Entity\Replication */
    $row['replication_status'] = $this
      ->getReplicationStatusIcon($entity
      ->getReplicationStatus(), $entity
      ->id());
    $row['name'] = $entity
      ->label();
    $row['source'] = $entity
      ->get('source')->entity ? $entity
      ->get('source')->entity
      ->label() : $this
      ->t('<em>Unknown</em>');
    $row['target'] = $entity
      ->get('target')->entity ? $entity
      ->get('target')->entity
      ->label() : $this
      ->t('<em>Unknown</em>');
    $user = User::load($entity->uid->target_id);
    $row['changed'] = $formatter
      ->format($entity
      ->getChangedTime());
    $row['created'] = $formatter
      ->format($entity
      ->getCreatedTime());
    $row['user'] = $user
      ->getAccountName();
    $row['description'] = $entity->description->value;

    // Set operations.
    $links = [];
    if ($entity
      ->hasLinkTemplate('delete-form') && in_array($entity
      ->getReplicationStatus(), [
      Replication::REPLICATED,
      Replication::FAILED,
    ])) {
      $links['delete'] = [
        'title' => t('Delete'),
        'url' => $entity
          ->toUrl('delete-form', [
          'absolute' => TRUE,
        ]),
      ];
    }
    $row[] = [
      'data' => [
        '#type' => 'operations',
        '#links' => $links,
      ],
    ];
    return $row;
  }

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

    // Determine when cron last ran.
    $cron_last = \Drupal::state()
      ->get('system.cron_last');
    if (!is_numeric($cron_last)) {
      $cron_last = \Drupal::state()
        ->get('install_time', 0);
    }
    $build = [];
    $build['#markup'] = '';
    if (\Drupal::state()
      ->get('workspace.last_replication_failed', FALSE)) {
      $message = $this
        ->generateMessageRenderArray('warning', $this
        ->t('Creation of new deployments has been blocked due to a failure. Contact your administrator to get the issue resolved and deployments unblocked.'));
      $user_has_access = \Drupal::currentUser()
        ->hasPermission('administer site configuration');
      if ($user_has_access) {
        $message = $this
          ->generateMessageRenderArray('warning', $this
          ->t('Creation of new deployments has been blocked due to a failure. After resolving the issue, go to the <a href="@url">replication settings</a> page to unblock deployments.', [
          '@url' => '/admin/config/replication/settings',
        ]));
      }
      elseif ($support_email = Settings::get('support_email_address', NULL)) {
        $message = $this
          ->generateMessageRenderArray('warning', $this
          ->t('Creating new deployments is not allowed at the moment. Please contact the <a href="mailto:@url">support team</a> to unblock creating new content deployments.', [
          '@url' => $support_email,
        ]));
      }
      $build['#markup'] .= \Drupal::service('renderer')
        ->render($message);
    }
    $build['#markup'] .= $this
      ->t('Last cron ran @time ago', [
      '@time' => \Drupal::service('date.formatter')
        ->formatTimeDiffSince($cron_last),
    ]);
    $build += parent::render();
    return $build;
  }

  /**
   * Loads entity IDs using a pager sorted by the entity id.
   *
   * @return array
   *   An array of entity IDs.
   */
  protected function getEntityIds() {
    $query = $this
      ->getStorage()
      ->getQuery()
      ->sort('changed', 'DESC');

    // Only add the pager if a limit is specified.
    if ($this->limit) {
      $query
        ->pager($this->limit);
    }
    return $query
      ->execute();
  }
  protected function getReplicationStatusIcon($status, $id) {
    $status = (int) $status;
    $icons = [
      Replication::QUEUED => $this
        ->t('&#x231A Queued'),
      Replication::REPLICATING => $this
        ->t('In progress'),
      Replication::REPLICATED => $this
        ->t('&#10004; Done'),
    ];
    if ($status === Replication::FAILED) {
      $link_url = Url::fromUserInput('/admin/structure/deployment/' . $id . '/fail-info');
      $link_url
        ->setOptions(array(
        'attributes' => array(
          'class' => array(
            'use-ajax',
          ),
          'data-dialog-type' => 'modal',
          'data-dialog-options' => Json::encode(array(
            'width' => 700,
          )),
        ),
      ));
      $icons[Replication::FAILED] = Link::fromTextAndUrl($this
        ->t('&#10006; Failed'), $link_url);
    }

    /** @var Replication $entity */
    $entity = $this
      ->getStorage()
      ->load($id);
    if ($status === Replication::QUEUED && !empty($entity
      ->getReplicationFailInfo())) {
      $link_url = Url::fromUserInput('/admin/structure/deployment/' . $id . '/requeue-info');
      $link_url
        ->setOptions(array(
        'attributes' => array(
          'class' => array(
            'use-ajax',
          ),
          'data-dialog-type' => 'modal',
          'data-dialog-options' => Json::encode(array(
            'width' => 700,
          )),
        ),
      ));
      $icons[Replication::QUEUED] = Link::fromTextAndUrl($this
        ->t('&#x231A Queued'), $link_url);
    }
    return $icons[$status];
  }

  /**
   * Generate a message render array with the given text.
   *
   * @param string $type
   *   The type of message: status, warning, or error.
   * @param string $message
   *   The message to create with.
   *
   * @return array
   *   The render array for a status message.
   *
   * @see \Drupal\Core\Render\Element\StatusMessages
   */
  protected function generateMessageRenderArray($type, $message) {
    return [
      '#theme' => 'status_messages',
      '#message_list' => [
        $type => [
          Markup::create($message),
        ],
      ],
    ];
  }

}

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::$limit protected property The number of entities to list per page, or FALSE to list all entities. 3
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::getDefaultOperations protected function Gets this list's default operations. 2
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::getTitle protected function Gets the title of the page. 1
EntityListBuilder::load public function Loads entities of this type from storage for listing. Overrides EntityListBuilderInterface::load 4
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.
ReplicationListBuilder::buildHeader public function Builds the header row for the entity listing. Overrides EntityListBuilder::buildHeader
ReplicationListBuilder::buildRow public function Builds a row for an entity in the entity listing. Overrides EntityListBuilder::buildRow
ReplicationListBuilder::generateMessageRenderArray protected function Generate a message render array with the given text.
ReplicationListBuilder::getEntityIds protected function Loads entity IDs using a pager sorted by the entity id. Overrides EntityListBuilder::getEntityIds
ReplicationListBuilder::getReplicationStatusIcon protected function
ReplicationListBuilder::render public function Builds the entity listing as renderable array for table.html.twig. Overrides EntityListBuilder::render
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.