Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.2 kB
2
Indexable
Never
/**
 * Custom filter handler for filtering by file name.
 *
 * @ViewsFilter("custom_filter_handler")
 */
class CustomFilterHandler extends StringFilter {

  /**
   * {@inheritdoc}
   */
  public function query() {
    $this->ensureMyTable();

    // First, join the media__field_media_document table.
    $this->query->addRelationship(
      'media__field_media_document',
      'field_media_document_target_id',
      [
        'table' => 'file_managed',
        'field' => 'fid',
        'left_table' => 'node__field_attachments', // Adjust this if needed.
        'left_field' => 'field_attachments_target_id', // Adjust this if needed.
      ]
    );

    // Then, join the file_managed table.
    $this->query->addRelationship(
      'file_managed',
      'fid',
      [
        'table' => 'media__field_media_document',
        'field' => 'field_media_document_target_id',
      ]
    );
    
    if (!empty($this->value)) {
      $escaped_value = Database::getConnection()->escapeLike($this->value);
      $this->query->addWhere(0, "file_managed.filename LIKE :file_name", [':file_name' => '%' . $escaped_value . '%']);
    }
  }
}