Add content type selector

This commit is contained in:
2026-01-23 22:18:32 -06:00
parent 016c42b717
commit bfadcd1f0b

View File

@@ -3,6 +3,9 @@
namespace Drupal\archive_tree\Plugin\Block; namespace Drupal\archive_tree\Plugin\Block;
use Drupal\Core\Block\BlockBase; use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\Entity\NodeType;
/** /**
* Provides an 'Archive tree' block. * Provides an 'Archive tree' block.
@@ -12,21 +15,35 @@ use Drupal\Core\Block\BlockBase;
* admin_label = @Translation("Archive tree"), * admin_label = @Translation("Archive tree"),
* ) * )
*/ */
use Drupal\Core\Block\BlockForm;
use Drupal\Core\Form\FormStateInterface;
class ArchiveTreeBlock extends BlockBase { class ArchiveTreeBlock extends BlockBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function defaultConfiguration() { public function defaultConfiguration() {
return [ return [
'expand_years' => FALSE, 'expand_years' => FALSE,
'content_types' => ['article'],
] + parent::defaultConfiguration(); ] + parent::defaultConfiguration();
} }
public function blockForm($form, FormStateInterface $form_state) { public function blockForm($form, FormStateInterface $form_state) {
$form = parent::blockForm($form, $form_state); $form = parent::blockForm($form, $form_state);
$types = NodeType::loadMultiple();
$options = [];
foreach ($types as $type) {
$options[$type->id()] = $type->label();
}
$form['content_types'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Content types to include'),
'#options' => $options,
'#default_value' => $this->configuration['content_types'],
'#description' => $this->t('Select one or more content types to include in the archive tree.'),
'#required' => TRUE,
];
$form['expand_years'] = [ $form['expand_years'] = [
'#type' => 'checkbox', '#type' => 'checkbox',
'#title' => $this->t('Expand all years by default'), '#title' => $this->t('Expand all years by default'),
@@ -36,15 +53,25 @@ class ArchiveTreeBlock extends BlockBase {
return $form; return $form;
} }
public function blockSubmit($form, FormStateInterface $form_state) { public function blockSubmit($form, FormStateInterface $form_state) {
parent::blockSubmit($form, $form_state); parent::blockSubmit($form, $form_state);
$this->configuration['expand_years'] = $form_state->getValue('expand_years'); $this->configuration['expand_years'] = $form_state->getValue('expand_years');
$selected_types = array_filter($form_state->getValue('content_types'));
$this->configuration['content_types'] = $selected_types;
} }
public function build() { public function build() {
$storage = \Drupal::entityTypeManager()->getStorage('node'); $storage = \Drupal::entityTypeManager()->getStorage('node');
$types = array_filter($this->configuration['content_types']);
if (empty($types)) {
// If no types selected, return empty block.
return [
'#markup' => $this->t('No content types selected.'),
];
}
$query = $storage->getQuery() $query = $storage->getQuery()
->condition('type', 'article') ->condition('type', $types, 'IN')
->condition('status', 1) ->condition('status', 1)
->sort('created', 'DESC') ->sort('created', 'DESC')
->accessCheck(TRUE); ->accessCheck(TRUE);