1

I have an exposed "List Price" field in a View using the "in between" operator (so that users can search for content between this price and that price), and I need to add the text "Min Price" and "Max Price" as placeholder text for the Min and Max fields. I had success using the custom module below to add placeholder text to other exposed fields using the "is equal to" operator, but I have had no luck using the same module code to affect the min/max fields. Here is my working module code:

<?php
function bootstrap_primary_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == "views_exposed_form") {
    if (isset($form['field_productname_value'])) {
            $form['field_productname_value']['#attributes'] = array('placeholder' => array(t('Product Name')));
    }
  }
 }
?>

and here is the code that does NOT work for the "min" portion of the price field:

<?php
function bootstrap_primary_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == "views_exposed_form") {
    if (isset($form['field_listprice_value[min]'])) {
            $form['field_listprice_value[min]']['#attributes'] = array('placeholder' => array(t('Min Price')));
    }
  }
 }
?>

Can someone please guide me on what I am doing wrong and how I can make it work? Could it possibly me the "[min]" portion of the module code that is tripping things up?

In case it helps, here is the full code for the List Price field:

<div class="views-exposed-widget views-widget-filter-field_listprice_value" id="edit-field-listprice-value-wrapper">
                  <label for="edit-field-listprice-value">
            List Price          </label>
                        <div class="views-widget">
          <div class="form-type-textfield form-item-field-listprice-value-min form-item form-group">
  <input type="text" maxlength="128" size="30" value="" name="field_listprice_value[min]" id="edit-field-listprice-value-min" class="form-control form-text">
</div>
<div class="form-type-textfield form-item-field-listprice-value-max form-item form-group">
  <label for="edit-field-listprice-value-max">And </label>
 <input type="text" maxlength="128" size="30" value="" name="field_listprice_value[max]" id="edit-field-listprice-value-max" class="form-control form-text">
</div>
        </div>
              </div>

...and here is the full code for the field that the module worked on:

<div class="views-exposed-widget views-widget-filter-field_productname_value" id="edit-field-productname-value-wrapper">
                  <label for="edit-field-productname-value">
            Product Name          </label>
                        <div class="views-widget">
          <div class="form-type-textfield form-item-field-productname-value form-item form-group">
 <input type="text" maxlength="128" size="30" value="" name="field_productname_value" id="edit-field-productname-value" class="form-control form-text" placeholder="Product Name">
</div>
        </div>

Thanks!

1 Answer 1

3

Found the solution!!! (here: https://www.drupal.org/node/2280069#comment-8844805)

  if (isset($form['field_listprice_value']['min'])) {
    $form['field_listprice_value']['min']['#attributes'] = array('placeholder' => array(t('Min Price')));
  }
  if (isset($form['field_listprice_value']['max'])) {
    $form['field_listprice_value']['max']['#attributes'] = array('placeholder' => array(t('Max Price')));
  }
1
  • Thank you! Exactly what I was looking for! It's always a very good idea to post the answer once you know it! :) Oct 18, 2016 at 15:49

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.