Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
948 B
2
Indexable
use Drupal\views\Plugin\views\filter\InOperator;

/**
 * Implements hook_views_data_alter().
 */
function custom_filter_views_data_alter(array &$data) {
  $data['node_field_data']['custom_file_filter'] = [
    'title' => t('Custom File Filter'),
    'filter' => [
      'id' => 'custom_filter_handler',
    ],
  ];
}

/**
 * Custom filter handler for filtering by file name.
 */
class CustomFilterHandler extends InOperator {

  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) . '%']);
    }
  }
}