You are here

class views_merge_rows_plugin_display_extender in Views Merge Rows 7

The plugin that merges rows with the same content in the specified fields.

Hierarchy

Expanded class hierarchy of views_merge_rows_plugin_display_extender

1 string reference to 'views_merge_rows_plugin_display_extender'
views_merge_rows_views_plugins in ./views_merge_rows.views.inc
Implements hook_views_plugins().

File

./views_merge_rows_plugin_display_extender.inc, line 12
Contains the class to extend views display with rows merge functionality.

View source
class views_merge_rows_plugin_display_extender extends views_plugin_display_extender {

  /**
   * Provide a list of options for this plugin.
   */
  public function options_definition_alter(&$options) {
    $options['merge_rows'] = array(
      'default' => FALSE,
      'bool' => TRUE,
    );
    $options['field_config'] = array(
      'default' => array(),
    );
  }

  /**
   * Returns configuration for row merging.
   *
   * Only returns the configuration for the fields present in the view.
   * If a new field was added to the view, the default configuration for this
   * field is returned.
   *
   * @return array
   *   Configuration for row merging.
   */
  public function get_options() {
    if ($this->display->display->handler
      ->uses_fields()) {
      $options = array();
      $options['merge_rows'] = $this->display
        ->get_option('merge_rows');
      if (empty($options['merge_rows'])) {
        $options['merge_rows'] = FALSE;
      }
      $options['field_config'] = array();
      $field_config = $this->display
        ->get_option('field_config');
      $fields = $this->display->display->handler
        ->get_option('fields');
      foreach ($fields as $field => $info) {
        if (isset($field_config[$field])) {
          $options['field_config'][$field] = $field_config[$field];
        }
        else {
          $options['field_config'][$field] = array(
            'merge_option' => 'merge_unique',
            'separator' => ', ',
          );
        }
      }
    }
    else {
      $options['merge_rows'] = FALSE;
      $options['field_config'] = array();
    }
    return $options;
  }

  /**
   * Provide a form to edit options for this plugin.
   */
  protected function views_merge_rows_options_form(&$form, &$form_state) {
    $options = $this
      ->get_options();
    if ($this->display->display->handler
      ->use_pager()) {
      $form['warning_markup'] = array(
        '#markup' => '<div class="warning messages">' . t('It is highly recommended to disable pager if you merge rows.') . '</div>',
      );
    }
    $form['#tree'] = TRUE;
    $form['#theme'] = 'views_merge_rows_display_extender_plugin_table';
    $form['#title'] .= t('Merge rows with the same content.');
    $form['merge_rows'] = array(
      '#type' => 'checkbox',
      '#title' => t('Merge rows with the same content in the specified fields'),
      '#default_value' => $options['merge_rows'],
    );

    // Create an array of allowed columns from the data we know:
    $field_names = $this->display->display->handler
      ->get_field_labels();
    foreach ($field_names as $field => $name) {
      $safe = str_replace(array(
        '][',
        '_',
        ' ',
      ), '-', $field);

      // Markup for the field name.
      $form['field_config'][$field]['name'] = array(
        '#markup' => $name,
      );

      // Select for merge options.
      $form['field_config'][$field]['merge_option'] = array(
        '#type' => 'select',
        '#options' => array(
          'merge_unique' => t('Merge unique values of this field'),
          'merge' => t('Merge values of this field'),
          'filter' => t('Use values of this field as a filter'),
          'first_value' => t('Use the first value of this field'),
          'count' => t('Count merged values of this field'),
          'count_unique' => t('Count merged unique values of this field'),
        ),
        '#default_value' => $options['field_config'][$field]['merge_option'],
      );
      $form['field_config'][$field]['separator'] = array(
        '#id' => 'views-merge-rows-separator',
        '#title' => t('Separator:'),
        '#type' => 'textfield',
        '#size' => 10,
        '#default_value' => $options['field_config'][$field]['separator'],
        '#dependency' => array(
          'edit-options-field-config-' . $safe . '-merge-option' => array(
            'merge',
            'merge_unique',
          ),
        ),
      );
    }
  }

  /**
   * Saves the row merge options.
   */
  protected function views_merge_rows_options_form_submit(&$form, &$form_state) {
    foreach ($form_state['values']['options'] as $option => $value) {
      $this->display
        ->set_option($option, $value);
    }
  }

  /**
   * Provide the form to set the rows merge options.
   */
  public function options_form(&$form, &$form_state) {
    switch ($form_state['section']) {
      case 'views_merge_rows':
        $this
          ->views_merge_rows_options_form($form, $form_state);
        break;
    }
  }

  /**
   * Saves the row merge options.
   */
  public function options_submit(&$form, &$form_state) {
    switch ($form_state['section']) {
      case 'views_merge_rows':
        $this
          ->views_merge_rows_options_form_submit($form, $form_state);
        break;
    }
  }

  /**
   * Provide the default summary for options in the views UI.
   */
  public function options_summary(&$categories, &$options) {
    if ($this->display->display->handler
      ->uses_fields()) {
      $configuration = $this
        ->get_options();
      $options['views_merge_rows'] = array(
        'category' => 'other',
        'title' => t('Merge rows'),
        'value' => $configuration['merge_rows'] ? t('Settings') : t('No'),
        'desc' => t('Allow merging rows with the same content in the specified fields.'),
      );
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
views_merge_rows_plugin_display_extender::get_options public function Returns configuration for row merging.
views_merge_rows_plugin_display_extender::options_definition_alter public function Provide a list of options for this plugin. Overrides views_plugin_display_extender::options_definition_alter
views_merge_rows_plugin_display_extender::options_form public function Provide the form to set the rows merge options. Overrides views_plugin_display_extender::options_form
views_merge_rows_plugin_display_extender::options_submit public function Saves the row merge options. Overrides views_plugin_display_extender::options_submit
views_merge_rows_plugin_display_extender::options_summary public function Provide the default summary for options in the views UI. Overrides views_plugin_display_extender::options_summary
views_merge_rows_plugin_display_extender::views_merge_rows_options_form protected function Provide a form to edit options for this plugin.
views_merge_rows_plugin_display_extender::views_merge_rows_options_form_submit protected function Saves the row merge options.
views_object::$definition public property Handler's definition.
views_object::$options public property Except for displays, options for the object will be held here. 1
views_object::altered_option_definition function Collect this handler's option definition and alter them, ready for use.
views_object::construct public function Views handlers use a special construct function. 4
views_object::destroy public function Destructor. 2
views_object::export_option public function 1
views_object::export_options public function
views_object::export_option_always public function Always exports the option, regardless of the default value.
views_object::options Deprecated public function Set default options on this object. 1
views_object::option_definition public function Information about options for all kinds of purposes will be held here. 13
views_object::set_default_options public function Set default options.
views_object::set_definition public function Let the handler know what its full definition is.
views_object::unpack_options public function Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away.
views_object::unpack_translatable public function Unpack a single option definition.
views_object::unpack_translatables public function Unpacks each handler to store translatable texts.
views_object::_set_option_defaults public function
views_plugin::$display public property The current used views display.
views_plugin::$plugin_name public property The plugin name of this plugin, for example table or full.
views_plugin::$plugin_type public property The plugin type of this plugin, for example style or query.
views_plugin::$view public property The top object of a view. Overrides views_object::$view 1
views_plugin::additional_theme_functions public function Provide a list of additional theme functions for the theme info page.
views_plugin::plugin_title public function Return the human readable name of the display.
views_plugin::summary_title public function Returns the summary of the settings in the display. 8
views_plugin::theme_functions public function Provide a full list of possible theme templates used by this style.
views_plugin::validate public function Validate that the plugin is correct and can be saved. 3
views_plugin_display_extender::defaultable_sections public function Static member function to list which sections are defaultable and what items each section contains.
views_plugin_display_extender::init public function
views_plugin_display_extender::options_validate public function Validate the options form. Overrides views_plugin::options_validate
views_plugin_display_extender::pre_execute public function Set up any variables on the view prior to execution.
views_plugin_display_extender::query public function Inject anything into the query that the display_extender handler needs. Overrides views_plugin::query