Commit 521dffaa authored by Alphons Jaimon's avatar Alphons Jaimon Committed by Hari Venu V
Browse files

Issue #3221669 by AJV009, Anjali Rathod: Create 'Node Type' filter

parent ecf6c4ed
Loading
Loading
Loading
Loading
+35 −7
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@
  use Drupal\Core\Form\FormBuilderInterface;
  use Symfony\Component\DependencyInjection\ContainerInterface;
  use Drupal\Component\Render\FormattableMarkup;
  use \Drupal\Core\TempStore\PrivateTempStoreFactory;

  /**
   * Returns responses for Nalog routes.
@@ -43,6 +44,13 @@
     */
    protected $formBuilder;

    /**
     * The tempstore service.
     *
     * @var PrivateTempStoreFactory;
     */
    protected $tempstore;

    /**
     * {@inheritdoc}
     */
@@ -50,7 +58,8 @@
      return new static(
        $container->get('database'),
        $container->get('date.formatter'),
        $container->get('form_builder')
        $container->get('form_builder'),
        $container->get('tempstore.private')
      );
    }

@@ -62,11 +71,12 @@
     * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
     *   The date formatter service.
     */
    public function __construct(Connection $database, DateFormatterInterface $date_formatter, FormBuilderInterface $form_builder) {
    public function __construct(Connection $database, DateFormatterInterface $date_formatter, FormBuilderInterface $form_builder, PrivateTempStoreFactory $temp_store_factory) {
      $this->database = $database;
      $this->dateFormatter = $date_formatter;
      $this->userStorage = $this->entityTypeManager()->getStorage('user');
      $this->formBuilder = $form_builder;
      $this->tempstore = $temp_store_factory->get('node_action_log');
    }

    /**
@@ -80,7 +90,8 @@
      $classes = static::getLogTypeClassMap();
      $typelabel = static::getTypeLabel();
      $rows = [];
      $filter_query = $this->buildFilterQuery();
      $filter_query = $this->buildFilterKeyQuery();
      $filter_query_node_type = $this->buildFilterNodeTypeQuery();
      $header = [
        [
          'data' => $this->t('ID'),
@@ -144,6 +155,9 @@
      if ($filter_query) {
        $query->condition('nal.type', $filter_query, 'IN');
      }
      if ($filter_query_node_type) {
        $query->condition('nal.bundle', $filter_query_node_type, 'IN');
      }
      $result = $query->execute();

      foreach ($result as $nalog) {
@@ -237,11 +251,25 @@
     *   An associative array with values only or NULL if there were
     *   no filters set.
     */
    protected function buildFilterQuery() {
      if (empty($_SESSION['nal_filter_key'])) {
        return;
    protected function buildFilterKeyQuery(): ?array {
      if (empty($this->tempstore->get('nal_filter_key'))) {
        return null;
      }
      return array_values($_SESSION['nal_filter_key']);
      return array_values($this->tempstore->get('nal_filter_key'));
    }

    /**
     * Builds a query for filters based on session.
     *
     * @return array|null
     *   An associative array with values only or void if there were
     *   no filters set.
     */
    protected function buildFilterNodeTypeQuery(): ?array
    {
      if (empty($this->tempstore->get('nal_filter_node_type'))) {
        return null;
      }
      return array_values($this->tempstore->get('nal_filter_node_type'));
    }
  }
+60 −8
Original line number Diff line number Diff line
@@ -4,6 +4,8 @@

  use Drupal\Core\Form\FormBase;
  use Drupal\Core\Form\FormStateInterface;
  use Drupal\Core\TempStore\PrivateTempStoreFactory;
  use Symfony\Component\DependencyInjection\ContainerInterface;

  /**
   * Provides the database logging filter form.
@@ -12,6 +14,29 @@
   */
  class FilterForm extends FormBase {

    /**
     * The tempstore service.
     *
     * @var PrivateTempStoreFactory;
     */
    protected $tempstore;

    /**
     * Class constructor.
     */
    public function __construct(PrivateTempStoreFactory $temp_store_factory) {
      $this->tempstore = $temp_store_factory->get('node_action_log');
    }

    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
      return new static(
        $container->get('tempstore.private')
      );
    }

    /**
     * {@inheritdoc}
     */
@@ -28,7 +53,6 @@
        'updated' => $this->t('Updated'),
        'deleted' => $this->t('Deleted'),
      ];

      $form['filters'] = [
        '#type' => 'details',
        '#title' => $this->t('Filter log messages'),
@@ -41,10 +65,33 @@
        '#size' => 8,
        '#options' => $filters,
      ];
      if (!empty($_SESSION['nal_filter_key'])) {
        $form['filters']['key']['#default_value'] = $_SESSION['nal_filter_key'];
      if (!is_null($this->tempstore->get('nal_filter_key'))) {
        $form['filters']['key']['#default_value'] = $this->tempstore->get('nal_filter_key');
      }

      $node_types = \Drupal::entityTypeManager()
        ->getStorage('node_type')
        ->loadMultiple();

      $filters_node_type = array();
      foreach( $node_types as $node_type ) {
        $filters_node_type[$node_type->getOriginalId()] = $this->t($node_type->label());
      }
      uasort($filters_node_type, static function ($a, $b) {
        return $a > $b;
      });
      $form['filters']['node_type'] = [
        '#title' => $this->t('Node Type'),
        '#type' => 'select',
        '#multiple' => TRUE,
        '#size' => 8,
        '#options' => $filters_node_type,
      ];
      if (!is_null($this->tempstore->get('nal_filter_node_type'))) {
        $form['filters']['node_type']['#default_value'] = $this->tempstore->get('nal_filter_node_type');
      }


      $form['filters']['actions'] = [
        '#type' => 'actions',
        '#attributes' => ['class' => ['container-inline']],
@@ -53,7 +100,7 @@
        '#type' => 'submit',
        '#value' => $this->t('Filter'),
      ];
      if (!empty($_SESSION['nal_filter_key'])) {
      if (!is_null($this->tempstore->get('nal_filter_key')) || !is_null($this->tempstore->get('nal_filter_node_type'))) {
        $form['filters']['actions']['reset'] = [
          '#type' => 'submit',
          '#value' => $this->t('Reset'),
@@ -68,8 +115,9 @@
     * {@inheritdoc}
     */
    public function validateForm(array &$form, FormStateInterface $form_state) {
      if ($form_state->isValueEmpty('key')) {
        $form_state->setErrorByName('key', $this->t('You must select atleast one option.'));
      if ($form_state->isValueEmpty('key') && $form_state->isValueEmpty('node_type')) {
        $form_state->setErrorByName('key', $this->t('You must select atleast any one of the filter option.'));
        $form_state->setErrorByName('node_type', $this->t('You must select atleast one of the filter option.'));
      }
    }

@@ -78,7 +126,10 @@
     */
    public function submitForm(array &$form, FormStateInterface $form_state) {
      if ($form_state->hasValue('key')) {
        $_SESSION['nal_filter_key'] = $form_state->getValue('key');
        $this->tempstore->set('nal_filter_key', $form_state->getValue('key'));
      }
      if ($form_state->hasValue('node_type')) {
        $this->tempstore->set('nal_filter_node_type', $form_state->getValue('node_type'));
      }
    }

@@ -91,7 +142,8 @@
     *   The current state of the form.
     */
    public function resetForm(array &$form, FormStateInterface $form_state) {
      $_SESSION['nal_filter_key'] = [];
      $this->tempstore->delete('nal_filter_key');
      $this->tempstore->delete('node_type');
    }

  }