1

I configured a exposed filter for a shipping date field on myview with operator "Is between". I saw two labels,"Start date" and "End date" in myview form. I wanna change these strings to "From Date: " and "To Date: ".

I have checked the file sites/all/modules/date/date_views/includes/date_views_filter_handler_simple.inc This file includes the

function date_parts_form()

that includes the codes:

$label = t('Start date');

and

$label = t('End date');

But I don't want to modify this file directly for further maintenance. I am not certain how to override this function with keeping an original module file. Please show me the way with circumstantial directory path and file name.

3 Answers 3

1

Now I use an Omega sub theme, mytheme. I added to a CSS file, mytheme-alpha-default.css, some styles such as the following:

label {display:none;}
div.some:before {content:"To Date: "; display:block; margin-left: 2.0em;}
1
  • just simple, light & perfect !
    – Kojo
    Commented Aug 28, 2015 at 10:20
1

You can override the handler used by the Date module using code similar to the following one.

mymodule.module

function mymodule_views_api() {
  return array(
    'api' => 3, 
    'path' => drupal_get_path('module', 'mymodule') . '/includes',
  );
}

function mymodule_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'views_field_views_data_alter') {
    $group = $implementations['mymodule'];
    unset($implementations['mymodule']);
    $implementations['mymodule'] = $group;
  }
}

function mymodule_views_field_views_data_alter(&$result, $field, $module) {
  if ($module == 'date') {
    foreach ($result as $table => $data) {
      $additional = array();
      $field_name = $field['field_name'];
      foreach ($data as $column => $value) {
        // For filters, arguments, and sorts, determine if this column is for
        // the start date ('value') or the end date ('value2').
        $this_column = NULL;
        foreach (array_keys($field['columns']) as $candidate_column) {
          if ($column == $field['field_name'] . '_' . $candidate_column) {
            $this_column = $candidate_column;
            break;
          }
        }

        // Only alter the date fields, not timezone, rrule, offset, etc.
        if ($this_column != 'value' && $this_column != 'value2') {
          continue;
        }

        // Replace the handler used by the Date module.
        if (array_key_exists('filter', $value)) {
          $result[$table][$column]['filter']['handler'] = 'mymodule_filter_handler_simple';
        }
      }
    }
  }
}

 includes/mymodule_filter_handler_simple.inc

class mymodule_filter_handler_simple extends date_views_filter_handler_simple {
  function date_parts_form($form_state, $prefix, $source, $which, $operator_values, $identifier, $relative_id) {
    module_load_include('inc', 'date_api', 'date_api_elements');
    switch ($prefix) {
      case 'min':
        $label = t('From date');
        $relative_label = t('Relative start date');
        break;
      case 'max':
        $label = t('To date');
        $relative_label = t('Relative end date');
        break;
      default:
        $label = '';
        $relative_label = t('Relative date');
        break;
    }

    $type = $this->options['form_type'];
    if ($type == 'date_popup' && !module_exists('date_popup')) {
      $type = 'date_text';
    }

    $format = $this->date_handler->views_formats($this->options['granularity'], 'sql');
    $granularity = array_keys($this->date_handler->date_parts($this->options['granularity']));
    $relative_value = ($prefix == 'max' ? $this->options['default_to_date'] : $this->options['default_date']);

    if (!empty($form_state['exposed'])) {
      // UI when the date selector is exposed.
      $default_date = $this->date_default_value($prefix);
      $id = 'edit-' . str_replace('_', '-', $this->field) . '-' . $prefix;
      $form[$prefix] = array(
        '#title' => check_plain($label),
        '#type' => $type,
        '#size' => 20,
        '#default_value' => !empty($this->value[$prefix]) ? $this->value[$prefix] : $default_date,
        '#date_format' => date_limit_format($format, $granularity),
        '#date_label_position' => 'within',
        '#date_year_range' => $this->options['year_range'],
        '#process' => array($type . '_element_process'),
        '#prefix' => '<div id="' . $id . '-wrapper"><div id="' . $id . '">',
        '#suffix' => '</div></div>',
      );
      if ($which == 'all') {
        $form[$prefix]['#pre_render'][] = 'ctools_dependent_pre_render';
        $form[$prefix]['#dependency'] = array($source => $operator_values);
      }
      if (!isset($form_state['input'][$identifier][$prefix])) {
        $form_state['input'][$identifier][$prefix] = $this->value[$prefix];
      }
    }
    else {
      // UI when the date selector is on the views configuration screen.
      $default_date = '';
      $id = 'edit-options-value-' . $prefix;
      $form[$prefix . '_group'] = array(
        '#type' => 'fieldset',
        '#attributes' => array('class' => array('date-views-filter-fieldset')),
      );
      $form[$prefix . '_group'][$prefix . '_choose_input_type'] = array(
        '#title' => check_plain($label),
        '#type' => 'select',
        '#options' => array('date' => t('Select a date'), 'relative' => ('Enter a relative date')),
        '#attributes' => array('class' => array($prefix . '-choose-input-type')),
        '#default_value' => !empty($relative_value) ? 'relative' : 'date',
      );
      $form[$prefix . '_group'][$prefix] = array(
        '#title' => t('Select a date'),
        '#type' => $type,
        '#size' => 20,
        '#default_value' => !empty($this->value[$prefix]) ? $this->value[$prefix] : $default_date,
        '#date_format' => date_limit_format($format, $granularity),
        '#date_label_position' => 'within',
        '#date_year_range' => $this->options['year_range'],
        '#process' => array($type . '_element_process'),
        '#prefix' => '<div id="' . $id . '-wrapper"><div id="' . $id . '">',
        '#suffix' => '</div></div>',
        '#states' => array(
          'visible' => array(
            ":input.{$prefix}-choose-input-type" => array('value' => 'date'),
          ),
        ),
      );
      $form[$prefix . '_group'][$relative_id] = array(
        '#type' => 'textfield',
        '#title' => check_plain($relative_label),
        '#default_value' => $relative_value,
        '#description' => t("Relative dates are computed when the view is displayed. Examples: now, now +1 day, 12AM today, Monday next week. <a href=\"@relative_format\">More examples of relative date formats in the PHP documentation</a>.", array('@relative_format' => 'http://www.php.net/manual/en/datetime.formats.relative.php')),
        '#states' => array(
          'visible' => array(
            ":input.{$prefix}-choose-input-type" => array('value' => 'relative'),
          ),
        ),
      );
      if ($which == 'all') {
        $form[$prefix . '_group']['#pre_render'][] = 'ctools_dependent_pre_render';
        $form[$prefix . '_group']['#dependency'] = array($source => $operator_values);
      }
    }
    return $form;
  }
}
0

The @kiamlaluno solution is good but I think this tiny problem not need this code you can handle and solve it with some jquery. it just need provide (or add to exists js file). in your js file do something like below

 $('.views-widget .form-item-created-min label').text("My min label text"); 
 $('.views-widget .form-item-created-max label').text("My max  ");

This is just example and you should get the class of your end and start form element(you can try it in console of your firebug first then if you see that work put it in scripts.js(or any js file you want))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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