diff --git a/src/Plugin/Block/ArchiveTreeBlock.php b/src/Plugin/Block/ArchiveTreeBlock.php index 9af9e79..d0f3ea3 100644 --- a/src/Plugin/Block/ArchiveTreeBlock.php +++ b/src/Plugin/Block/ArchiveTreeBlock.php @@ -3,6 +3,9 @@ namespace Drupal\archive_tree\Plugin\Block; use Drupal\Core\Block\BlockBase; +use Drupal\Core\Form\FormStateInterface; +use Drupal\node\Entity\NodeType; + /** * Provides an 'Archive tree' block. @@ -12,21 +15,35 @@ use Drupal\Core\Block\BlockBase; * admin_label = @Translation("Archive tree"), * ) */ -use Drupal\Core\Block\BlockForm; -use Drupal\Core\Form\FormStateInterface; class ArchiveTreeBlock extends BlockBase { /** * {@inheritdoc} */ + public function defaultConfiguration() { return [ 'expand_years' => FALSE, + 'content_types' => ['article'], ] + parent::defaultConfiguration(); } + public function blockForm($form, FormStateInterface $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'] = [ '#type' => 'checkbox', '#title' => $this->t('Expand all years by default'), @@ -36,15 +53,25 @@ class ArchiveTreeBlock extends BlockBase { return $form; } + public function blockSubmit($form, FormStateInterface $form_state) { parent::blockSubmit($form, $form_state); $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() { $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() - ->condition('type', 'article') + ->condition('type', $types, 'IN') ->condition('status', 1) ->sort('created', 'DESC') ->accessCheck(TRUE);