Browse Source

added bulk select

main
astanley 7 days ago
parent
commit
d704be1580
  1. 23
      src/Form/CollectionManagerForm.php

23
src/Form/CollectionManagerForm.php

@ -97,8 +97,8 @@ class CollectionManagerForm extends FormBase {
if ($all_descendants) { if ($all_descendants) {
$nodes = $node_storage->loadMultiple($all_descendants); $nodes = $node_storage->loadMultiple($all_descendants);
foreach ($nodes as $node) { foreach ($nodes as $node) {
if ($node->hasField('field_model')) { if ($node->hasField('field_models')) {
foreach ($node->get('field_model')->getValue() as $item) { foreach ($node->get('field_models')->getValue() as $item) {
if (!empty($item['target_id'])) { if (!empty($item['target_id'])) {
$available_model_tids[(int) $item['target_id']] = TRUE; $available_model_tids[(int) $item['target_id']] = TRUE;
} }
@ -172,22 +172,20 @@ class CollectionManagerForm extends FormBase {
]; ];
$header = [ $header = [
'select' => $this->t('Select'),
'title' => $this->t('Title'), 'title' => $this->t('Title'),
'nid' => $this->t('Node ID'), 'nid' => $this->t('Node ID'),
'model' => $this->t('Model'), 'model' => $this->t('Model'),
'status' => $this->t('Status'), 'status' => $this->t('Status'),
]; ];
$rows = []; $options = [];
foreach ($page_nodes as $nid => $node) { foreach ($page_nodes as $nid => $node) {
$models = []; $models = [];
if ($node->hasField('field_model')) { if ($node->hasField('field_models')) {
foreach ($node->get('field_model')->referencedEntities() as $term) { foreach ($node->get('field_models')->referencedEntities() as $term) {
$models[] = $term->label(); $models[] = $term->label();
} }
} }
$rows[$nid] = [ $options[$nid] = [
'select' => ['data' => ['#type' => 'checkbox', '#name' => 'selected[' . $nid . ']', '#return_value' => $nid]],
'title' => ['data' => $node->toLink($node->label())->toRenderable()], 'title' => ['data' => $node->toLink($node->label())->toRenderable()],
'nid' => $nid, 'nid' => $nid,
'model' => implode(', ', $models), 'model' => implode(', ', $models),
@ -195,10 +193,13 @@ class CollectionManagerForm extends FormBase {
]; ];
} }
// Use Drupal's tableselect element so the header includes the standard
// select-all checkbox. It selects/deselects all rows on the current page.
$form['results'] = [ $form['results'] = [
'#type' => 'table', '#type' => 'tableselect',
'#header' => $header, '#header' => $header,
'#rows' => $rows, '#options' => $options,
'#multiple' => TRUE,
'#empty' => $this->t('No descendants match the selected model filter.'), '#empty' => $this->t('No descendants match the selected model filter.'),
]; ];
$form['pager'] = ['#type' => 'pager']; $form['pager'] = ['#type' => 'pager'];
@ -268,7 +269,7 @@ class CollectionManagerForm extends FormBase {
} }
$action_id = (string) $form_state->getValue(['bulk', 'action']); $action_id = (string) $form_state->getValue(['bulk', 'action']);
$selected = array_keys(array_filter((array) $form_state->getValue('selected', []))); $selected = array_values(array_filter((array) $form_state->getValue('results', [])));
if (!$action_id || !$selected) { if (!$action_id || !$selected) {
$this->messenger()->addWarning($this->t('Select at least one result and an action.')); $this->messenger()->addWarning($this->t('Select at least one result and an action.'));
return; return;

Loading…
Cancel
Save