Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.1 kB
2
Indexable
<?php

namespace Drupal\custom_filter\Plugin\views\filter;

use Drupal\views\Plugin\views\filter\StringFilter;

/**
 * Custom filter handler for filtering by file name.
 *
 * @ViewsFilter("custom_filter_handler")
 */
class CustomFilterHandler extends StringFilter {

  /**
   * {@inheritdoc}
   */
  public function exposeOptions() {
    parent::exposeOptions();
    $this->options['expose']['use_operator'] = TRUE;
    $this->options['expose']['operator'] = $this->options['operator'];
    $this->options['expose']['label'] = 'Custom File Filter';
  }

  /**
   * {@inheritdoc}
   */
  public function query() {
    if (!empty($this->value)) {
      $this->ensureMyTable();
      $this->query->addWhere(0, "node.nid IN (
        SELECT entity_id FROM {node__field_attachments}
        INNER JOIN {file_managed} ON node__field_attachments.field_attachments_target_id = file_managed.fid
        WHERE file_managed.filename LIKE :file_name
      )", [':file_name' => '%' . db_like($this->value) . '%']);
    }
  }
}