1

I want to add new option "Own Company" into "Options" select in View filter settings form: enter image description here This is filter of entityreference field. Also I want to have opportunity alter views query with this value.

I tried with form_alter but I can't save chosen value.

Also I wanted to write own views child class that will handle entityreference filter settings. But I can't find dafault handler in hook_views_data_alter.

Also saw that views filter select in entityreference module is made with ctools plugins system. Filter settings form might be plugable but I can't manage to do it.

Has anybody altered views filter settings form?

4

1) Add new custom views filter handler that extends views_handler_filter_in_operator

class MY_MODULE_own_company_filter_handler extends views_handler_filter_in_operator {

  public function get_value_options() {
    $options = parent::get_value_options();
    $options['own'] = 'Own Company';
    $this->value_options = $options;

    return $this->value_options;
  }

  /**
   * Replaces "Own Company" value with real value. 
   * Do query alter here or in query() function.
   */
  function pre_query() {
    parent::pre_query();

    // Check if "Own Company" was chosen.
    if (!empty($this->value['own'])) {

      // Unset "Own Company" 'own' value.
      unset($this->value['own']);

      // Get current "Own Company" value the way you like.
      $company_id = 100500; // Example value.

      // Set current "Own Company" value.
      $this->value[$company_id] = $company_id;
    }
  }

}

2) Implement hook_views_data_alter() and add custom handler to all entityreference field filters

/**
 * Implements hook_views_data_alter().
 */
function MY_MODULE_views_data_alter(&$data) {
  // Loop all field filters.
  foreach ($data as $data_key => $data_field) {
    if (strpos($data_key, 'field_data_') !== FALSE) {

      // Get field name
      $field_name = str_replace('field_data_', '', $data_key);

      // Check if field filter is entity reference 
      if (isset($data_field[$field_name . '_target_id']['filter']['handler'])) {
        if ($data_field[$field_name . '_target_id']['filter']['handler'] == 'views_handler_filter_in_operator') {

          // Name of custom handler class
          $custom = 'MY_MODULE_own_company_filter_handler';
          // Add custom handler
          $data[$data_key][$field_name . '_target_id']['filter']['handler'] = $custom;

        }
      }
    }
  }
}
-2

What you might need is to create a "relationship" (http://gotdrupal.com/videos/drupal-views-relationships) with your user's profile and filter the user's "company" and have that match one of your company's taxonomy terms to get the same company results.

You might have to create a view's php field to get the result you want. (How to use Views PHP to rewrite results of a field?)

  • duckx, thanks for answer but totaly NO :) I need to alter views filter settings form. I need new option in select. Adding relations cant help with it. And adding php in fields can't halp me to. And I don't recommend you to use PHP filter. Storing php code in database it's very bad idea – milkovsky May 9 '13 at 14:37
  • yes thats the quick and dirty way if you don't have time to spare to get the output you need. or you can spend hours poking around at the api to see where to modify the form. its just an alternative solution. – Patoshi パトシ May 9 '13 at 14:45
  • yep, I need clean solution. Anyway PHP in view field doesn't allow to add new option in filter settings form and alter query. – milkovsky May 9 '13 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.