Untitled

mail@pastecode.io avatarunknown
plain_text
a month ago
1.5 kB
1
Indexable
Never
<?php

namespace Drupal\custom_filter\Plugin\views\filter;

use Drupal\views\Plugin\views\filter\InOperator;

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

  /**
   * {@inheritdoc}
   */
  public function getValueOptions() {
    // This method fetches the values for the exposed filter. It's not needed for your use case.
    // If you still need to include it for other use cases, feel free to keep it here.
    // If you're solely filtering based on user input and not fetching value options,
    // you can leave this method empty or remove it.
  }

  /**
   * {@inheritdoc}
   */
  public function query() {
    $user_input = $this->getExposedInput()['custom_filter'];

    if (!empty($user_input)) {
      $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($user_input) . '%']);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function adminSummary() {
    // This method provides a summary of the exposed filter's current configuration.
    // Since we're not using the getValueOptions() method, you can leave this method as-is.
    return '';
  }
}