Problem/Motivation

The Files administration page use a query object to select the files to display on the page. This query is executed from the form builder. Like any query, this one can be altered using hook_query_alter(). But since there is no tag or metadata attached to the query, it is hard to only alter this specific query. Also, while hook_form_FORM_ID_alter can be used to add element to this form (file_entity_admin_files), it cannot be easily used to add fields to filter the displayed files. there is no way to pass information from the form to the query alter hook implementation.

Proposed resolution

Adding a tag would ease usage of hook_query_alter() (or hook_query_TAG_alter) to alter the query used to select the displayed files. Adding the $form_state to the query metadata, would allow easy usage of submitted values when altering the query.

API changes

This proposed solution does not change the existing API. It provide a new hook to safely alter the administration page: hook_query_admin_files_alter (actually hook_query_TAG_alter).

Comments

pbuyle’s picture

Title: Ease alteration of the Files administration page query » Ease alteration of the Files administration page's query
Status: Active » Needs review
StatusFileSize
new963 bytes
pbuyle’s picture

As sample use case, with the patch in #1, the following code can be used to add some basic filter to the page:


function MODULE_form_file_entity_admin_files_alter(&$form, $form_state) {
  $form['filters'] = array(
    '#type' => 'fieldset',
    '#title' => t('Filters'),
    '#weight' => -99,
  );
  $form['filters']['type'] = array(
    '#type' => 'select',
    '#required' => FALSE,
    '#title' => 'File type',
    '#options' => array(NULL => t('<Any>')) + file_info_file_types_options_list(),
    '#default_value' => !empty($form_state['values']['type']) ? $form_state['values']['type'] : NULL,
  );
  $form['filters']['filename'] = array(
    '#type' => 'textfield',
    '#required' => FALSE,
    '#title' => 'File name',
    '#default_value' => !empty($form_state['values']['filename']) ? $form_state['values']['filename'] : NULL,
  );
  $form['filters']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Filter'),
    '#submit' => array('pwe_media_form_file_entity_admin_files_submit'),
    '#limit_validation_errors' => array(array('type'), array('filename')),
  );
}

function MODULE_form_file_entity_admin_files_submit($form, &$form_state) {
  $form_state['rebuild'] = TRUE;
}

function MODULE_query_admin_files_alter(QueryAlterableInterface $query) {
  $form_state = $query->getMetaData('form_state');
  if (!empty($form_state['values']['type'])) {
    $query->condition('f.type', $form_state['values']['type']);
  }
  if (!empty($form_state['values']['filename'])) {
    $query->condition('f.filename', "%{$form_state['values']['filename']}%", 'LIKE');
  }
}

dave reid’s picture

Version: 7.x-1.x-dev » 7.x-2.x-dev

File entity has switched to a 7.x-2.x branch and the 7.x-1.x branch is no longer used. Please make sure to update your Git clones.

pbuyle’s picture

Here is the updated patch.

acbramley’s picture

+1 for this feature, I needed a filename filter on this page and this patch allowed that easily (thanks for the example too btw :)

ParisLiakos’s picture

ParisLiakos’s picture

Status: Needs review » Fixed

yeah, very good for DX :)
thanks commited

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

Apply issue template.