I turned to this module after realizing that this was a much easier option than writing a custom module for all of the fields I needed to add placeholders to. This module works great for everything except one field. That is the exposed views filter I have created for users to type in their minimum and maximum price ranges for products listed (It is a views table of active products. I am using Drupal Commerce 2.0). I tried to use my custom module to overwrite this module for the placeholders for that field without luck. Here is the excerpt of code that I used for those two fields in my custom module to get the results I am looking for:
<?php
function MYTHEME_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'views_exposed_form'){
$form['commerce_price_amount']['#process'][] = 'placeholders_process_commerce_price_amount';
}
}
function MYMODULE_process_commerce_price_amount($element) {
$element['min']['#attributes']['placeholder'] = t( 'Min Price' );
$element['max']['#attributes']['placeholder'] = t( 'Max Price' );
return $element;
}
?>
And here is a view export to test with in an instance of Drupal Commerce Kickstart 2.0:
$view = new view();
$view->name = 'product_listing';
$view->description = 'A simple table style listing of all products available on the website.';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'Product listing';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Product listing';
$handler->display->display_options['use_ajax'] = TRUE;
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '20';
$handler->display->display_options['style_plugin'] = 'table';
/* Header: Global: Text area */
$handler->display->display_options['header']['area']['id'] = 'area';
$handler->display->display_options['header']['area']['table'] = 'views';
$handler->display->display_options['header']['area']['field'] = 'area';
$handler->display->display_options['header']['area']['label'] = 'Description';
$handler->display->display_options['header']['area']['content'] = 'This is the traditional style display to view all products in a table format. ';
$handler->display->display_options['header']['area']['format'] = 'full_html';
/* Relationship: Content: Referenced products */
$handler->display->display_options['relationships']['field_product_product_id']['id'] = 'field_product_product_id';
$handler->display->display_options['relationships']['field_product_product_id']['table'] = 'field_data_field_product';
$handler->display->display_options['relationships']['field_product_product_id']['field'] = 'field_product_product_id';
/* Field: Content: Title */
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';
$handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;
/* Field: Commerce Product: SKU */
$handler->display->display_options['fields']['sku']['id'] = 'sku';
$handler->display->display_options['fields']['sku']['table'] = 'commerce_product';
$handler->display->display_options['fields']['sku']['field'] = 'sku';
$handler->display->display_options['fields']['sku']['relationship'] = 'field_product_product_id';
$handler->display->display_options['fields']['sku']['link_to_product'] = 0;
/* Field: Commerce Product: Price */
$handler->display->display_options['fields']['commerce_price']['id'] = 'commerce_price';
$handler->display->display_options['fields']['commerce_price']['table'] = 'field_data_commerce_price';
$handler->display->display_options['fields']['commerce_price']['field'] = 'commerce_price';
$handler->display->display_options['fields']['commerce_price']['relationship'] = 'field_product_product_id';
$handler->display->display_options['fields']['commerce_price']['element_type'] = '0';
$handler->display->display_options['fields']['commerce_price']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['commerce_price']['element_wrapper_type'] = '0';
$handler->display->display_options['fields']['commerce_price']['element_default_classes'] = FALSE;
$handler->display->display_options['fields']['commerce_price']['click_sort_column'] = 'amount';
$handler->display->display_options['fields']['commerce_price']['settings'] = array(
'calculation' => '0',
);
$handler->display->display_options['fields']['commerce_price']['field_api_classes'] = TRUE;
/* Field: Commerce Product revision: Status */
$handler->display->display_options['fields']['status']['id'] = 'status';
$handler->display->display_options['fields']['status']['table'] = 'commerce_product_revision';
$handler->display->display_options['fields']['status']['field'] = 'status';
$handler->display->display_options['fields']['status']['relationship'] = 'field_product_product_id';
$handler->display->display_options['fields']['status']['type'] = 'custom';
$handler->display->display_options['fields']['status']['type_custom_true'] = 'Available';
$handler->display->display_options['fields']['status']['type_custom_false'] = 'Out of Stock';
$handler->display->display_options['fields']['status']['not'] = 0;
/* Sort criterion: Content: Title */
$handler->display->display_options['sorts']['title']['id'] = 'title';
$handler->display->display_options['sorts']['title']['table'] = 'node';
$handler->display->display_options['sorts']['title']['field'] = 'title';
$handler->display->display_options['sorts']['title']['order'] = 'DESC';
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
'product_display' => 'product_display',
);
/* Filter criterion: Commerce Product: Title */
$handler->display->display_options['filters']['title']['id'] = 'title';
$handler->display->display_options['filters']['title']['table'] = 'commerce_product';
$handler->display->display_options['filters']['title']['field'] = 'title';
$handler->display->display_options['filters']['title']['relationship'] = 'field_product_product_id';
$handler->display->display_options['filters']['title']['operator'] = 'contains';
$handler->display->display_options['filters']['title']['exposed'] = TRUE;
$handler->display->display_options['filters']['title']['expose']['operator_id'] = 'title_op';
$handler->display->display_options['filters']['title']['expose']['label'] = 'Search by Title';
$handler->display->display_options['filters']['title']['expose']['operator'] = 'title_op';
$handler->display->display_options['filters']['title']['expose']['identifier'] = 'title';
$handler->display->display_options['filters']['title']['expose']['remember_roles'] = array(
2 => '2',
1 => 0,
3 => 0,
);
/* Filter criterion: Commerce Product: SKU */
$handler->display->display_options['filters']['sku']['id'] = 'sku';
$handler->display->display_options['filters']['sku']['table'] = 'commerce_product';
$handler->display->display_options['filters']['sku']['field'] = 'sku';
$handler->display->display_options['filters']['sku']['relationship'] = 'field_product_product_id';
$handler->display->display_options['filters']['sku']['operator'] = 'contains';
$handler->display->display_options['filters']['sku']['exposed'] = TRUE;
$handler->display->display_options['filters']['sku']['expose']['operator_id'] = 'sku_op';
$handler->display->display_options['filters']['sku']['expose']['label'] = 'Search by SKU';
$handler->display->display_options['filters']['sku']['expose']['operator'] = 'sku_op';
$handler->display->display_options['filters']['sku']['expose']['identifier'] = 'sku';
$handler->display->display_options['filters']['sku']['expose']['remember_roles'] = array(
2 => '2',
1 => 0,
3 => 0,
);
/* Filter criterion: Commerce Product: Price (commerce_price:amount) */
$handler->display->display_options['filters']['commerce_price_amount']['id'] = 'commerce_price_amount';
$handler->display->display_options['filters']['commerce_price_amount']['table'] = 'field_data_commerce_price';
$handler->display->display_options['filters']['commerce_price_amount']['field'] = 'commerce_price_amount';
$handler->display->display_options['filters']['commerce_price_amount']['relationship'] = 'field_product_product_id';
$handler->display->display_options['filters']['commerce_price_amount']['operator'] = 'between';
$handler->display->display_options['filters']['commerce_price_amount']['exposed'] = TRUE;
$handler->display->display_options['filters']['commerce_price_amount']['expose']['operator_id'] = 'commerce_price_amount_op';
$handler->display->display_options['filters']['commerce_price_amount']['expose']['label'] = 'Price Range';
$handler->display->display_options['filters']['commerce_price_amount']['expose']['operator'] = 'commerce_price_amount_op';
$handler->display->display_options['filters']['commerce_price_amount']['expose']['identifier'] = 'commerce_price_amount';
$handler->display->display_options['filters']['commerce_price_amount']['expose']['remember_roles'] = array(
2 => '2',
1 => 0,
3 => 0,
);
/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'listing';
Let me know if there is any additional information I can add to be more helpful.
| Comment | File | Size | Author |
|---|
Comments
Comment #1
ericshell commentedComment #2
rafalenden commentedHi,
After importing your view, field with label "And" (max) worked well, but for field "Price Range" (min) didn't worked because #title attribute for minimum value is empty.
Try to add this simple form_alter().
Comment #3
ericshell commentedThanks Rafal! That worked perfectly. For those that come across this post in the future, this is what I ended up using for both the min and max price:
I later noticed that the search bar was doing the same exact thing. In case you are looking to change that as well, place this in your tempalte.php or custom module along with the previous function.
Comment #4
ericshell commentedComment #5
ericshell commentedComment #6
rafalenden commentedComment #7
hockey2112 commented#2... perfect solution! Thanks! It was driving me nuts, not being able to form_alter the min max price fields.