You are here

class NodeOrderListBuilder in Node Order 8

Defines a class to build a listing of node entities.

Hierarchy

Expanded class hierarchy of NodeOrderListBuilder

1 file declares its use of NodeOrderListBuilder
NodeOrderListController.php in src/Controller/NodeOrderListController.php

File

src/NodeOrderListBuilder.php, line 22

Namespace

Drupal\nodeorder
View source
class NodeOrderListBuilder extends EntityListBuilder implements FormInterface {

  /**
   * The key to use for the form element containing the entities.
   *
   * @var string
   */
  protected $entitiesKey = 'entities';

  /**
   * The entities being listed.
   *
   * @var \Drupal\Core\Entity\EntityInterface[]
   */
  protected $entities = [];

  /**
   * Current taxonomy term.
   *
   * @var \Drupal\taxonomy\Entity\Term
   */
  protected $taxonomyTerm;

  /**
   * The current primary database.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * The form builder service.
   *
   * @var \Drupal\Core\Form\FormBuilderInterface
   */
  protected $formBuilder;

  /**
   * The cache tags invalidator.
   *
   * @var \Drupal\Core\Cache\CacheTagsInvalidator
   */
  protected $cacheTagsInvalidator;

  /**
   * Default cache bin.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cacheDefault;

  /**
   * Nodes weight.
   *
   * @var array
   */
  private $nodesWeight = [];

  /**
   * Selectable entities total number.
   *
   * @var int
   */
  private $entitiesCount = 0;

  /**
   * {@inheritdoc}
   */
  public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, Connection $database, FormBuilderInterface $form_builder, CacheTagsInvalidator $cache_tags_invalidator, CacheBackendInterface $cache_default, Term $taxonomy_term) {
    parent::__construct($entity_type, $storage);
    $this->database = $database;
    $this->formBuilder = $form_builder;
    $this->taxonomyTerm = $taxonomy_term;
    $this->cacheTagsInvalidator = $cache_tags_invalidator;
    $this->cacheDefault = $cache_default;
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type, Term $taxonomy_term = NULL) {
    return new static($entity_type, $container
      ->get('entity.manager')
      ->getStorage($entity_type
      ->id()), $container
      ->get('database'), $container
      ->get('form_builder'), $container
      ->get('cache_tags.invalidator'), $container
      ->get('cache.default'), $taxonomy_term);
  }

  /**
   * {@inheritdoc}
   */
  protected function getEntityIds() {
    $taxonomy_indexes = $this->database
      ->select('taxonomy_index', 'ti');
    $taxonomy_indexes
      ->condition('ti.tid', $this->taxonomyTerm
      ->id());
    $count_query = clone $taxonomy_indexes;
    $count_query
      ->addExpression('Count(ti.nid)');

    /** @var \Drupal\Core\Database\Query\PagerSelectExtender $paged_query */
    $paged_query = $taxonomy_indexes
      ->extend(PagerSelectExtender::class);
    $paged_query
      ->limit($this->limit);
    $paged_query
      ->setCountQuery($count_query);
    $paged_query
      ->fields('ti', [
      'nid',
      'weight',
    ]);
    $paged_query
      ->orderBy('ti.weight');
    $this->nodesWeight = $paged_query
      ->execute()
      ->fetchAllKeyed();
    $this->entitiesCount = (int) $count_query
      ->execute()
      ->fetchField();

    // Method load will be triggered later, if we pass empty array as an argument,
    // method will be load all entities with type node, will be passed -1 as an
    // allowed ID for node to prevent loading all entities.
    // Case for term without nodes.
    return $this->nodesWeight ? array_keys($this->nodesWeight) : [
      -1,
    ];
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header = [
      'title' => $this
        ->t('Title'),
      'type' => [
        'data' => $this
          ->t('Content type'),
        'class' => [
          RESPONSIVE_PRIORITY_MEDIUM,
        ],
      ],
      'status' => $this
        ->t('Status'),
    ];
    $header['weight'] = $this
      ->t('Weight');
    return $header + parent::buildHeader();
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity, $weight_delta = 0) {

    /** @var \Drupal\node\NodeInterface $entity */
    $row['title']['data'] = [
      '#type' => 'link',
      '#title' => $entity
        ->label(),
      '#url' => $entity
        ->toUrl(),
    ];

    /** @var \Drupal\node\Entity\NodeType $type */
    $type = $entity->type->entity;
    $row['type'] = [
      '#markup' => $type
        ->label(),
    ];
    $published = $entity
      ->isPublished() ? $this
      ->t('Published') : $this
      ->t('Not published');
    $row['status'] = [
      '#markup' => $published,
    ];
    $row['#attributes']['class'][] = 'draggable';
    $row['#weight'] = $this->nodesWeight[$entity
      ->id()];
    $row['weight'] = [
      '#type' => 'weight',
      '#title' => $this
        ->t('Weight for @title', [
        '@title' => $entity
          ->label(),
      ]),
      '#title_display' => 'invisible',
      '#delta' => $weight_delta,
      '#default_value' => $this->nodesWeight[$entity
        ->id()],
      '#attributes' => [
        'class' => [
          'weight',
        ],
      ],
    ];
    return $row + parent::buildRow($entity);
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    return $this->formBuilder
      ->getForm($this);
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['#title'] = $this
      ->t('Order nodes for <em>%term_name</em>', [
      '%term_name' => $this->taxonomyTerm
        ->label(),
    ]);
    $form['term_id'] = [
      '#type' => 'value',
      '#value' => $this->taxonomyTerm
        ->id(),
    ];
    $form[$this->entitiesKey] = [
      '#type' => 'table',
      '#header' => $this
        ->buildHeader(),
      '#empty' => $this
        ->t('There is no @label yet.', [
        '@label' => $this->entityType
          ->getLabel(),
      ]),
      '#tabledrag' => [
        [
          'action' => 'order',
          'relationship' => 'sibling',
          'group' => 'weight',
        ],
      ],
    ];
    $this->entities = $this
      ->load();
    $weight_delta = ceil($this->entitiesCount / 2);
    foreach ($this->entities as $entity) {
      if ($row = $this
        ->buildRow($entity, $weight_delta)) {
        $form[$this->entitiesKey][$entity
          ->id()] = $row;
      }
    }
    if ($this->limit) {
      $form['pager'] = [
        '#type' => 'pager',
      ];
    }
    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Save'),
      '#button_type' => 'primary',
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    if (empty($form_state
      ->getValue('term_id'))) {
      $form_state
        ->setError($form, $this
        ->t('Term ID required.'));
    }
    if (empty($form_state
      ->getValue($this->entitiesKey))) {
      $form_state
        ->setErrorByName($this->entitiesKey, $this
        ->t('There are no nodes attached to current term.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $term_id = $form_state
      ->getValue('term_id');
    $nodes = $form_state
      ->getValue($this->entitiesKey);
    $nids = array_keys($nodes);
    $tags = [];
    $entities = $this->storage
      ->loadMultiple($nids);
    foreach ($entities as $nid => $node) {

      // Only take form elements that are blocks.
      if (isset($this->entities[$nid]) && $this->nodesWeight[$nid] !== $nodes[$nid]['weight']) {
        $this->database
          ->update('taxonomy_index')
          ->fields([
          'weight' => $nodes[$nid]['weight'],
        ])
          ->condition('tid', $term_id)
          ->condition('nid', $nid)
          ->execute();
        $tags = array_merge($tags, $node
          ->getCacheTags());
      }
    }
    $this
      ->messenger()
      ->addStatus($this
      ->t('The node orders have been updated.'));
    if (!empty($tags)) {
      $this->cacheTagsInvalidator
        ->invalidateTags($tags);
    }
    $this->cacheDefault
      ->deleteAll();
  }

}

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::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
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
NodeOrderListBuilder::$cacheDefault protected property Default cache bin.
NodeOrderListBuilder::$cacheTagsInvalidator protected property The cache tags invalidator.
NodeOrderListBuilder::$database protected property The current primary database.
NodeOrderListBuilder::$entities protected property The entities being listed.
NodeOrderListBuilder::$entitiesCount private property Selectable entities total number.
NodeOrderListBuilder::$entitiesKey protected property The key to use for the form element containing the entities.
NodeOrderListBuilder::$formBuilder protected property The form builder service.
NodeOrderListBuilder::$nodesWeight private property Nodes weight.
NodeOrderListBuilder::$taxonomyTerm protected property Current taxonomy term.
NodeOrderListBuilder::buildForm public function Form constructor. Overrides FormInterface::buildForm
NodeOrderListBuilder::buildHeader public function Builds the header row for the entity listing. Overrides EntityListBuilder::buildHeader
NodeOrderListBuilder::buildRow public function Builds a row for an entity in the entity listing. Overrides EntityListBuilder::buildRow
NodeOrderListBuilder::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityListBuilder::createInstance
NodeOrderListBuilder::getEntityIds protected function Loads entity IDs using a pager sorted by the entity id. Overrides EntityListBuilder::getEntityIds
NodeOrderListBuilder::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
NodeOrderListBuilder::render public function Builds the entity listing as renderable array for table.html.twig. Overrides EntityListBuilder::render
NodeOrderListBuilder::submitForm public function Form submission handler. Overrides FormInterface::submitForm
NodeOrderListBuilder::validateForm public function Form validation handler. Overrides FormInterface::validateForm
NodeOrderListBuilder::__construct public function Constructs a new EntityListBuilder object. Overrides EntityListBuilder::__construct
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.