You are here

class DomainListBuilder in Domain Access 8

User interface for the domain overview screen.

Hierarchy

Expanded class hierarchy of DomainListBuilder

File

domain/src/DomainListBuilder.php, line 21

Namespace

Drupal\domain
View source
class DomainListBuilder extends DraggableListBuilder {

  /**
   * {@inheritdoc}
   */
  protected $entitiesKey = 'domains';

  /**
   * The current user object.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The redirect destination helper.
   *
   * @var \Drupal\Core\Routing\RedirectDestinationInterface
   */
  protected $destinationHandler;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The domain entity access control handler.
   *
   * @var \Drupal\domain\DomainAccessControlHandler
   */
  protected $accessHandler;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The Domain storage handler.
   *
   * @var \Drupal\domain\DomainStorageInterface
   */
  protected $domainStorage;

  /**
   * The domain field element manager.
   *
   * @var \Drupal\domain\DomainElementManagerInterface
   */
  protected $domainElementManager;

  /**
   * The User storage handler.
   *
   * @var \Drupal\user\UserStorageInterface
   */
  protected $userStorage;

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static($entity_type, $container
      ->get('entity_type.manager')
      ->getStorage($entity_type
      ->id()), $container
      ->get('current_user'), $container
      ->get('redirect.destination'), $container
      ->get('entity_type.manager'), $container
      ->get('module_handler'), $container
      ->get('domain.element_manager'));
  }

  /**
   * Constructs a new DomainListBuilder object.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   * @param \Drupal\domain\DomainStorageInterface $domain_storage
   *   The domain storage class.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The active user account.
   * @param \Drupal\Core\Routing\RedirectDestinationInterface $destination_handler
   *   The redirect destination helper.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\domain\DomainElementManagerInterface $domain_element_manager
   *   The domain field element manager.
   */
  public function __construct(EntityTypeInterface $entity_type, DomainStorageInterface $domain_storage, AccountInterface $account, RedirectDestinationInterface $destination_handler, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, DomainElementManagerInterface $domain_element_manager) {
    parent::__construct($entity_type, $domain_storage);
    $this->entityTypeId = $entity_type
      ->id();
    $this->domainStorage = $domain_storage;
    $this->entityType = $entity_type;
    $this->currentUser = $account;
    $this->destinationHandler = $destination_handler;
    $this->entityTypeManager = $entity_type_manager;
    $this->accessHandler = $this->entityTypeManager
      ->getAccessControlHandler('domain');
    $this->moduleHandler = $module_handler;
    $this->domainElementManager = $domain_element_manager;
    $this->userStorage = $this->entityTypeManager
      ->getStorage('user');

    // DraggableListBuilder sets this to FALSE, which cancels any pagination.
    $this->limit = 50;
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'domain_admin_overview_form';
  }

  /**
   * {@inheritdoc}
   */
  public function getOperations(EntityInterface $entity) {
    $operations = parent::getOperations($entity);
    $destination = $this->destinationHandler
      ->getAsArray();
    $default = $entity
      ->isDefault();
    $id = $entity
      ->id();

    // If the user cannot edit domains, none of these actions are permitted.
    $access = $this->accessHandler
      ->checkAccess($entity, 'update');
    if ($access
      ->isForbidden()) {
      return $operations;
    }
    $super_admin = $this->currentUser
      ->hasPermission('administer domains');
    if ($super_admin || $this->currentUser
      ->hasPermission('access inactive domains')) {
      if ($entity
        ->status() && !$default) {
        $operations['disable'] = [
          'title' => $this
            ->t('Disable'),
          'url' => Url::fromRoute('domain.inline_action', [
            'op' => 'disable',
            'domain' => $id,
          ]),
          'weight' => 50,
        ];
      }
      elseif (!$default) {
        $operations['enable'] = [
          'title' => $this
            ->t('Enable'),
          'url' => Url::fromRoute('domain.inline_action', [
            'op' => 'enable',
            'domain' => $id,
          ]),
          'weight' => 40,
        ];
      }
    }
    if (!$default && $super_admin) {
      $operations['default'] = [
        'title' => $this
          ->t('Make default'),
        'url' => Url::fromRoute('domain.inline_action', [
          'op' => 'default',
          'domain' => $id,
        ]),
        'weight' => 30,
      ];
    }
    if (!$default && $this->accessHandler
      ->checkAccess($entity, 'delete')
      ->isAllowed()) {
      $operations['delete'] = [
        'title' => $this
          ->t('Delete'),
        'url' => Url::fromRoute('entity.domain.delete_form', [
          'domain' => $id,
        ]),
        'weight' => 20,
      ];
    }
    $operations += $this->moduleHandler
      ->invokeAll('domain_operations', [
      $entity,
      $this->currentUser,
    ]);
    foreach ($operations as $key => $value) {
      if (isset($value['query']['token'])) {
        $operations[$key]['query'] += $destination;
      }
    }

    /** @var DomainInterface $default */
    $default = $this->domainStorage
      ->loadDefaultDomain();

    // Deleting the site default domain is not allowed.
    if ($default && $id == $default
      ->id()) {
      unset($operations['delete']);
    }
    return $operations;
  }

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header['label'] = $this
      ->t('Name');
    $header['hostname'] = $this
      ->t('Hostname');
    $header['status'] = $this
      ->t('Status');
    $header['is_default'] = $this
      ->t('Default');
    $header['scheme'] = $this
      ->t('Scheme');
    $header += parent::buildHeader();
    if (!$this->currentUser
      ->hasPermission('administer domains')) {
      unset($header['weight']);
    }
    return $header;
  }

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

    // If the user cannot view the domain, none of these actions are permitted.
    $admin = $this->accessHandler
      ->checkAccess($entity, 'view');
    if ($admin
      ->isForbidden()) {
      return;
    }
    $row['label'] = $entity
      ->label();
    $row['hostname'] = [
      '#markup' => $entity
        ->getLink(),
    ];
    if ($entity
      ->isActive()) {
      $row['hostname']['#prefix'] = '<strong>';
      $row['hostname']['#suffix'] = '</strong>';
    }
    $row['status'] = [
      '#markup' => $entity
        ->status() ? $this
        ->t('Active') : $this
        ->t('Inactive'),
    ];
    $row['is_default'] = [
      '#markup' => $entity
        ->isDefault() ? $this
        ->t('Yes') : $this
        ->t('No'),
    ];
    $row['scheme'] = [
      '#markup' => $entity
        ->getRawScheme(),
    ];
    $row += parent::buildRow($entity);
    if ($entity
      ->getRawScheme() === 'variable') {
      $row['scheme']['#markup'] .= ' (' . $entity
        ->getScheme(FALSE) . ')';
    }
    if (!$this->currentUser
      ->hasPermission('administer domains')) {
      unset($row['weight']);
    }
    return $row;
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);
    $form[$this->entitiesKey]['#domains'] = $this->entities;
    $form['actions']['submit']['#value'] = $this
      ->t('Save configuration');

    // Only super-admins may sort domains.
    if (!$this->currentUser
      ->hasPermission('administer domains')) {
      $form['actions']['submit']['#access'] = FALSE;
      unset($form['#tabledrag']);
    }

    // Delta is set after each row is loaded.
    $count = count($this->domainStorage
      ->loadMultiple()) + 1;
    foreach (Element::children($form['domains']) as $key) {
      if (isset($form['domains'][$key]['weight'])) {
        $form['domains'][$key]['weight']['#delta'] = $count;
      }
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   *
   * Overrides the parent method to prevent saving bad data.
   *
   * @link https://www.drupal.org/project/domain/issues/2925798
   * @link https://www.drupal.org/project/domain/issues/2925629
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    foreach ($form_state
      ->getValue($this->entitiesKey) as $id => $value) {
      if (isset($this->entities[$id]) && $this->entities[$id]
        ->get($this->weightKey) != $value['weight']) {

        // Reset weight properly.
        $this->entities[$id]
          ->set($this->weightKey, $value['weight']);

        // Do not allow accidental hostname rewrites.
        $this->entities[$id]
          ->set('hostname', $this->entities[$id]
          ->getCanonical());
        $this->entities[$id]
          ->save();
      }
    }
  }

  /**
   * Internal sort method for form weights.
   */
  private function sortByWeight($a, $b) {
    if ($a['weight'] < $b['weight']) {
      return 0;
    }
    return 1;
  }

  /**
   * {@inheritdoc}
   *
   * Builds the entity listing as a form with pagination. This method overrides
   * both Drupal\Core\Config\Entity\DraggableListBuilder::render() and
   * Drupal\Core\Entity\EntityListBuilder::render().
   */
  public function render() {

    // Build the default form, which includes weights.
    $form = $this
      ->formBuilder()
      ->getForm($this);

    // Only add the pager if a limit is specified.
    if ($this->limit) {
      $form['pager'] = [
        '#type' => 'pager',
      ];
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   *
   * Loads entity IDs using a pager sorted by the entity weight. The default
   * behavior when using a limit is to sort by id.
   *
   * We also have to limit by assigned domains of the active user.
   *
   * See Drupal\Core\Entity\EntityListBuilder::getEntityIds()
   *
   * @return array
   *   An array of entity IDs.
   */
  protected function getEntityIds() {
    $query = $this
      ->getStorage()
      ->getQuery()
      ->sort($this->entityType
      ->getKey('weight'));

    // If the user cannot administer domains, we must filter the query further
    // by assigned IDs. We don't have to check permissions here, because that is
    // handled by the route system and buildRow(). There are two permissions
    // that allow users to view the entire list.
    if (!$this->currentUser
      ->hasPermission('administer domains') && !$this->currentUser
      ->hasPermission('view domain list')) {
      $user = $this->userStorage
        ->load($this->currentUser
        ->id());
      $allowed = $this->domainElementManager
        ->getFieldValues($user, DomainInterface::DOMAIN_ADMIN_FIELD);
      $query
        ->condition('id', array_keys($allowed), 'IN');
    }

    // Only add the pager if a limit is specified.
    if ($this->limit) {
      $query
        ->pager($this->limit);
    }
    return $query
      ->execute();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigEntityListBuilder::getDefaultOperations public function Gets this list's default operations. Overrides EntityListBuilder::getDefaultOperations 15
ConfigEntityListBuilder::load public function Loads entities of this type from storage for listing. Overrides EntityListBuilder::load 7
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
DomainListBuilder::$accessHandler protected property The domain entity access control handler.
DomainListBuilder::$currentUser protected property The current user object.
DomainListBuilder::$destinationHandler protected property The redirect destination helper.
DomainListBuilder::$domainElementManager protected property The domain field element manager.
DomainListBuilder::$domainStorage protected property The Domain storage handler.
DomainListBuilder::$entitiesKey protected property The key to use for the form element containing the entities. Overrides DraggableListBuilder::$entitiesKey
DomainListBuilder::$entityTypeManager protected property The entity type manager.
DomainListBuilder::$moduleHandler protected property The module handler. Overrides EntityHandlerBase::$moduleHandler
DomainListBuilder::$userStorage protected property The User storage handler.
DomainListBuilder::buildForm public function Form constructor. Overrides DraggableListBuilder::buildForm
DomainListBuilder::buildHeader public function Builds the header row for the entity listing. Overrides DraggableListBuilder::buildHeader
DomainListBuilder::buildRow public function Builds a row for an entity in the entity listing. Overrides DraggableListBuilder::buildRow
DomainListBuilder::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityListBuilder::createInstance
DomainListBuilder::getEntityIds protected function Loads entity IDs using a pager sorted by the entity weight. The default behavior when using a limit is to sort by id. Overrides EntityListBuilder::getEntityIds
DomainListBuilder::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
DomainListBuilder::getOperations public function Provides an array of information to build a list of operation links. Overrides EntityListBuilder::getOperations
DomainListBuilder::render public function Builds the entity listing as a form with pagination. This method overrides both Drupal\Core\Config\Entity\DraggableListBuilder::render() and Drupal\Core\Entity\EntityListBuilder::render(). Overrides DraggableListBuilder::render
DomainListBuilder::sortByWeight private function Internal sort method for form weights.
DomainListBuilder::submitForm public function Overrides the parent method to prevent saving bad data. Overrides DraggableListBuilder::submitForm
DomainListBuilder::__construct public function Constructs a new DomainListBuilder object. Overrides DraggableListBuilder::__construct
DraggableListBuilder::$entities protected property The entities being listed. 1
DraggableListBuilder::$formBuilder protected property The form builder.
DraggableListBuilder::$limit protected property The number of entities to list per page, or FALSE to list all entities. Overrides EntityListBuilder::$limit
DraggableListBuilder::$weightKey protected property Name of the entity's weight field or FALSE if no field is provided.
DraggableListBuilder::formBuilder protected function Returns the form builder.
DraggableListBuilder::validateForm public function Form validation handler. Overrides FormInterface::validateForm 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::ensureDestination protected function Ensures that a destination is present on the given URL.
EntityListBuilder::getLabel Deprecated protected function Gets the label of an entity.
EntityListBuilder::getStorage public function Gets the entity storage. Overrides EntityListBuilderInterface::getStorage
EntityListBuilder::getTitle protected function Gets the title of the page. 1
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.