Change default '-Any-' in Drupal 7 exposed views dropdown selector filter - drupal-7

I'd like to change the text of the default '-Any-' that Drupal 7 views uses for an exposed dropdown filter.
Based on an answer in this thread,
How to change the label of the default value (-Any-) of an exposed filter in Drupal Views?
I have created a module called any_exposed with a hook form alter:
function any_exposed_form_alter(&$form, &$form_state, $form_id) {
if ($form['#id'] == 'views-exposed-form-vendors-page') {
$form['field_vendor_type_tid']['#options']['ALL'] = t('Everything'); } }
But all that does is add another option for 'Everything' in the dropdown, it does not overwrite/translate '-Any-'. Just to test I added:
$form['submit']['#value'] = t('Search');
Which changes the text of the Submit button from 'Apply' to 'Search', and this works fine. In case you can't tell, I'm not much of a programmer, but I figure I must be missing something simple. Any help would be appreciated!

This is an old post but in case you are still looking or for anybody who comes to this searching for an answer. 'ALL' must be 'All' as in
$form['field_vendor_type_tid']['#options']['All'] = t('Everything');
Since the array has a member 'All' but not 'ALL' (case is important here) you are adding a member 'ALL' while you want to overwrite 'All'.

Use hook_form_views_exposed_form_alter instead hook_form_alter.
function hook_form_views_exposed_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'views_exposed_form') {
$form['tid']['#options']['All'] = t('Search');
}
}

function any_exposed_form_alter(&$form, &$form_state, $form_id) {
if ($form['#id'] == 'views-exposed-form-vendors-page') {
$form['field_vendor_type_tid']['#options']['ALL'] = t('Everything'); } }
Works perfectly after changing ALL to All.

I recommend using Better Exposed Filters module, it allows you to do this simply via the Views UI interface.
Install & enable the module
Edit your view, then click on 'Exposed form > Exposed form style'
Select 'Better Exposed Filters'
Click 'More options'
Change the value of 'Override "Any" option label'

Related

ExtJS Issue with boolean data and grid column list filter as well as Ext.Data.Store

I am using ExtJS 6 (although from what I can tell it applies up to version 7.4 as well) and I have a grid with a booleancolumn xtype. For that boolean column I wanted to use the filter list option. Yes I know there is a boolean filter option however I don't like how it works using a radio button. I wanted to be able to select the Yes or No with checkboxes, however I found that only the option with true as the value worked. Here is my column config:
{
header: 'Active'
, dataIndex: 'inactive'
, xtype: 'booleancolumn'
, trueText: 'No'
, falseText: 'Yes'
, filter:{
type: 'list',
options: [[true,"No"],[false, "Yes"]]
}
}
This didn't work when excluding the 'options' property and letting it get the data from the store either by the way. After looking through the code I discovered that it takes the 'options' config and creates its own Ext.Data.Store using that as the data. See here as a simple example that can be run that will get the same issue:
var teststore = new Ext.data.Store({
fields: [
'id',
'text'
],
data: [[true,"No"],[false, "Yes"]]
});
The problem is that the 'false' boolean value is changed and is replaced with a dynamically created generic id. I discovered the issue lays in the constructor for 'Ext.data.Model' for the following line:
if (!(me.id = id = data[idProperty]) && id !== 0) {
If that line evaluates to true it will replace the id with the generated one. To fix this I just added ' && id !== false' to the end of the if statement and this fixed the issue.
I have not tested this fully, however the logic seems sound and it looks like the same type of issue occurred with the value of '0' hence the ' && id !==0'. Since we are directed here from the sencha forums I wanted to bring this up in case it helps someone.
Since my post already has the answer I will post a better way to do it other than directly changing the Ext code file (whether this is the proper way I may be wrong). Instead, you can create a new js file that will need to be included in your application (you can name it ext-overrides.js). In the new js file you need only type:
Ext.override(Ext.data.Model, {
constructor: function(data, session) {
.....
}
}
You would then copy the constructor function code from your version of the ExtJS code in where the '.....' is (if perchance the constructor function arguments are different you would have to update those as well) and just add the suggested change I made above in the 'question'. A search of the Extjs code for Ext.define('Ext.data.Model' should get you there easily and then just scroll to the constructor function.

Override core functionality in Drupal 7

is there a way to cleanly override the function "form_execute_handlers(...)" which is found in /includes/form.inc ?
The problem is that there are some handler-functions like "user_profile_form_validate(...)" in /modules/user/user.pages.inc which cannot be found by the core version of "form.inc", since the following statement is missing in "form_execute_handlers(...)" for this special case:
module_load_include('inc', 'user', 'user.pages');
I would like to add that somehow, and therefore override form.inc ;)
Ok, I found a way to include the library (inside my custom module):
function wr_pages_init() {
if (($_GET['q'] == 'system/ajax' || strstr($_GET['q'], 'file/ajax/')) && $_POST['form_id'] == "user_profile_form") {
module_load_include('inc', 'user', 'user.pages');
}
}
Never change core functionality! Updating drupal will override your changes and is not good practice at all. Keep in mind that all other modules uses core too so things goes really wrong if you mess with core.
You can do custom user form like this (link to other answer):
drupal 7 cusomized user profile template can not save changes
There is also hooks for altering form handling. So you can change user form validation like this:
hook_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'user_profile_form') {
$form['#validate'][] = 'your_validation_function';
}
}
Or if you want only to use your own validation change:
$form['#validate'] = array('your_validation_function');
You don't have to check queries when including user library. Just include it like:
function wr_pages_init() {
module_load_include('inc', 'user', 'user.pages');
// And other includes (if needed) same way.. like:
// Add jquery ui libraries..
drupal_add_library('system', 'ui');
drupal_add_library('system', 'ui.sortable');
drupal_add_library('system', 'ui.datepicker');
// Add ajax..
drupal_add_library('system', 'drupal.ajax');
// Some own JS
drupal_add_js(drupal_get_path('module', 'wr_pages') . '/js/mysuper.js', 'file');
}

drupal 7 make field required in form_user_profile_form_alter

I use Drupal 7.x.
I add several fields in the user profile form.
In the registration i don't need them, but in the first login it must be filled.
I can't use Profile2 module.
In my module:
function mymodule_form_user_profile_form_alter(&$form, &$form_state) {
$form['field_test_field']['#required'] = 'TRUE';
}
The function is fired, but don't make it required.
I tried also:
$form['field_test_field'][0]['#required'] = 'TRUE';
$form['#field_info']['field_test_field']['required'] = '1';
How can make an user_profile_form field Required in the form alter?
The following example can be used for text field:
$form['field_full_name'][LANGUAGE_NONE]['0']['value']['#required'] = TRUE;
Second example is for email field:
$form['field_custom_email_address'][LANGUAGE_NONE]['0']['email']['#required'] = TRUE;
You're currently setting #required to a string (using single quotes) instead the boolean it needs to be. Give this a go $form['field_test_field']['#required'] = true;
I found the solution: How to disable a field or make it readonly in Drupal 7
$form['field_secured_title']['und']['0']['value']['#attributes']['disabled'] = TRUE;
This worked.
But if i use multi language will it still work?
sorry for my English.

How to disable a field or make it readonly in Drupal 7

I am trying to disable couple of fields and make them readonly via hook_page_alter(). I was able to do check if user is viewing the page edit section (the form edit)
$page['content']['system_main']['#node_edit_form'] == TRUE)
then when I tried to disable couple of fields, I found that select list can be disabled by this code:
$page['content']['system_main']['field_my_field_name_a_select_list']['und']['#attributes']['disabled'] = TRUE;
but if I use the following code it doesn't work:
$page['content']['system_main']['field_my_field_name_a_select_list']['und']['#disabled'] = TRUE;
I also found that I can not use the same code to disable a text area field:
$page['content']['system_main']['field_my_text_area']['und']['#attributes']['disabled'] = TRUE;
The above code doesn't disable the text area, but the same code can disable the select list!
Then I tried hook_form_alter() to do the same thing, and I was able to disable fields and when I checked the rendered array from $page array, I saw that it shows:
$page['content']['system_main']['field_my_field_name_a_select_list']['und']['#disabled'] = TRUE;
but when I set the same code in hook_page_alter(), it didn't work. Looks like something else will override it, I thought that hook_page_alter() is the last place to change markup.
Any idea what is the best way to disable/readonly any kind of field, inside hook_page_alter() in drupal 7?
Thank you
It works for text fields^
$form['field_secured_title']['und']['0']['value']['#attributes']['disabled'] = TRUE;
Like it said in the docs
You can use attributes :
$form['#attributes'] = array('disabled' => TRUE);

how to prevent an element to validate

hi to all i am using cakephp framework for my project. in on of my form i take a checkbox on click on this two other text box are shown.
by using cakephp validates method i validate the form data but i want that when the checkbox is not checked then it avoid the validation for that text box. it check only when the checkbox is checked.
so plz help me.
thanks in advance
You can use your models beforeValidate servicecall for that and add extra validation criteria for this model.
For example:
function beforeValidate($options = array())
{
if(!empty($this->data['Model']['fieldOne']))
$this->validate['fieldTwo'] = array(/*normal validation rules*/);
return true; // Needed or validation fails
}
You can use custom validation methods:
var $validate = array(
'checkbox1' => 'checkboxRule'
);
// If checkbox1 is checked, requires checkbox2 to be checked as well
function checkboxRule() {
if (!empty($this->data[$this->alias]['checkbox1'])) {
return !empty($this->data[$this->alias]['checkbox2']);
}
return true;
}
You can also invalidate other fields, like checkbox2, at the same time by calling $this->invalidate('checkbox2') in your custom method.
Also, you can unset the validation in your controller like this:
unset($this->Model->validate);

Resources