class NodeOrderListBuilder in Node Order 8
Defines a class to build a listing of node entities.
Hierarchy
- class \Drupal\Core\Entity\EntityHandlerBase uses DependencySerializationTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityListBuilder implements EntityHandlerInterface, EntityListBuilderInterface uses MessengerTrait, RedirectDestinationTrait
- class \Drupal\nodeorder\NodeOrderListBuilder implements FormInterface
- class \Drupal\Core\Entity\EntityListBuilder implements EntityHandlerInterface, EntityListBuilderInterface uses MessengerTrait, RedirectDestinationTrait
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\nodeorderView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityHandlerBase:: |
protected | property | The module handler to invoke hooks on. | 2 |
EntityHandlerBase:: |
protected | function | Gets the module handler. | 2 |
EntityHandlerBase:: |
public | function | Sets the module handler for this handler. | |
EntityListBuilder:: |
protected | property | Information about the entity type. | |
EntityListBuilder:: |
protected | property | The entity type ID. | |
EntityListBuilder:: |
protected | property | The number of entities to list per page, or FALSE to list all entities. | 3 |
EntityListBuilder:: |
protected | property | The entity storage class. | 1 |
EntityListBuilder:: |
public | function | Builds a renderable list of operation links for the entity. | 2 |
EntityListBuilder:: |
protected | function | Ensures that a destination is present on the given URL. | |
EntityListBuilder:: |
protected | function | Gets this list's default operations. | 2 |
EntityListBuilder:: |
protected | function | Gets the label of an entity. | |
EntityListBuilder:: |
public | function |
Provides an array of information to build a list of operation links. Overrides EntityListBuilderInterface:: |
2 |
EntityListBuilder:: |
public | function |
Gets the entity storage. Overrides EntityListBuilderInterface:: |
|
EntityListBuilder:: |
protected | function | Gets the title of the page. | 1 |
EntityListBuilder:: |
public | function |
Loads entities of this type from storage for listing. Overrides EntityListBuilderInterface:: |
4 |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
NodeOrderListBuilder:: |
protected | property | Default cache bin. | |
NodeOrderListBuilder:: |
protected | property | The cache tags invalidator. | |
NodeOrderListBuilder:: |
protected | property | The current primary database. | |
NodeOrderListBuilder:: |
protected | property | The entities being listed. | |
NodeOrderListBuilder:: |
private | property | Selectable entities total number. | |
NodeOrderListBuilder:: |
protected | property | The key to use for the form element containing the entities. | |
NodeOrderListBuilder:: |
protected | property | The form builder service. | |
NodeOrderListBuilder:: |
private | property | Nodes weight. | |
NodeOrderListBuilder:: |
protected | property | Current taxonomy term. | |
NodeOrderListBuilder:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
NodeOrderListBuilder:: |
public | function |
Builds the header row for the entity listing. Overrides EntityListBuilder:: |
|
NodeOrderListBuilder:: |
public | function |
Builds a row for an entity in the entity listing. Overrides EntityListBuilder:: |
|
NodeOrderListBuilder:: |
public static | function |
Instantiates a new instance of this entity handler. Overrides EntityListBuilder:: |
|
NodeOrderListBuilder:: |
protected | function |
Loads entity IDs using a pager sorted by the entity id. Overrides EntityListBuilder:: |
|
NodeOrderListBuilder:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
NodeOrderListBuilder:: |
public | function |
Builds the entity listing as renderable array for table.html.twig. Overrides EntityListBuilder:: |
|
NodeOrderListBuilder:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
NodeOrderListBuilder:: |
public | function |
Form validation handler. Overrides FormInterface:: |
|
NodeOrderListBuilder:: |
public | function |
Constructs a new EntityListBuilder object. Overrides EntityListBuilder:: |
|
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |