Hide a field collection field in an embedded form - drupal

I am trying to hide a field collection field that renders in an embedded form. This form renders on a few different nodes but only some nodes need all the fields, so I would like to hide them on ones that don't.
Right now I am trying to do it vis hook form alter but it doesn't work inside the field collection, it will work on a form rendered normally that shares fields with the collection.
function zenstrap_form_alter(&$form,&$form_state,$form_id){
//Form we want to target
case ($form_id==="coh_pow_node_form"):
//Appears in Normal form and Field Collection
//Hides in normal
$form['field_last_name']['#access']=FALSE;
//Appears in Normal form and Field Collection
//Hides in normal
$form['field_street']['#access']=FALSE;
//Appears in Field Collection
//Does nothing
$form['field_veteran_retired']['#access']=FALSE;
break;
}

To Hide field collection fields check the code below.
function YOURMODULE_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'YOURFORMID') {
$delta = 0;
$max_delta = $form['field_YOUR_field_collection'][LANGUAGE_NONE]['#max_delta'];
while ($delta <= $max_delta) {
$form['field_YOUR_field_collection'][LANGUAGE_NONE][$delta]['field_YOURfield'][LANGUAGE_NONE][0]['#access'] = FALSE;
$delta++;
}
}
}
Hope it helps you...

Related

Drupal 8 - Ignore contextual filter

On a search view, linked to search api and facets, I want to add contextual filters on my content type.
This content type have referenced entity fields, linked to taxonomy term (one field to taxonomy from destination vocabulary, the other to taxonomy from activity vocabulary)
So, I have created 2 contexual filter, one for each "taxonomy field".
view
filter
But, I appears only the first filter (destination) is applied. If my taxonomy term from URL is a destination, the view displays right results. But if it's an activity, it display all contents. So I supposed there is a problem with contextual filter validator : 'Action to take if filter value does not validate' should have something like 'Ignore filter' option, because with 'Display all results for the specified field', its shows everything, but don't execute next filter.
Anyone has a solution ?
Thanks a lot
Finally, I found a solution, just altering view in pre_build
/**
* Implements hook_views_pre_build().
*/
function my_module_views_pre_build(ViewExecutable $view)
{
if ($view->id() == 'tour_search' && $view->current_display == 'tours_taxonomy') {
$tid = reset($view->args);
if (! $tid) {
return;
}
/** #var Term $term */
$term = Term::load($tid);
if ($term->getVocabularyId() === 'activities') {
unset($view->argument['field_tour_destination']);
return;
}
if ($term->getVocabularyId() === 'destinations') {
unset($view->argument['field_tour_activity']);
return;
}
return;
}
}
I moved validation logic in this hook, by unsetting filter in with a simple term vocabulary test.
Hope it will helps somebody !

Get the value of the selected field in Drupal Form API

I have a custom content type called events which has a few fields defined in it.
The field name is field_store_name. I can get all the options from these check boxes using this code:
$form['field_store']['und']['#options']
This is how I get the option(s) that are selected/checked. Is this the correct way of doing this?
$form_state['build_info']['args']['0']->field_store['und']
Thanks
When user submits form your custom submitter could be called.
To add custom form submitter to any form you should use:
/* Implements hook_form_alter(). */
function moduleName_form_alter($form, $form_state) {
// ...
$form['#submit'][] = 'moduleName_submitterName';
// ...
}
So in custom submitter you will have all submitted values under $form_state['values']:
function moduleName_submitterName($form, $form_state) {
dpm($form_state['values']);
}
This index will apper in $form_state array only when you submit form and will contain submitted values. $form array will still contain default values shown at form before you've changed them and submitted form.
Read more:
An example of form submitter: https://www.drupal.org/node/717740.
hook_form_alter(): https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_form_alter/7
Value you need should be $form_state['values']['field_store]['und'][0].

alter "required" property of the form element in drupal 7

I have some require fields in some form,I want them be required in somewhere and in another place not be required (I want exactly them , maybe you suggest me use existance field in cck but this fields not in cck.)
I used profile2 module with some profile type, I want in profile type edit page as example name field be required and in user registration name field not be required.
I try to alter #require property of name field in form alter, it's changed correctly but in validation tell me the name field is required.
in
function mymodule_form_alter(&$form,&$form_state) {
if ($form_id == 'user_register_form') {
$form['profile_detailp']['field_name']['und']['#required']=0;
...
}
}
any idea ?
Your method is ok, and have to works but maybe in another module it require again.
function mymodule_form_alter(&$form,&$form_state) {
if ($form_id == 'user_register_form') {
$form['profile_detailp']['field_name']['und']['#required']=0;
...
}
}
Text field need do it like this
function mymodule_form_alter(&$form,&$form_state) {
if ($form_id == 'user_register_form') {
$form['profile_detailp']['field_name']['und'][0]['value']['#required']=0;
}
}

changing drupal 6 form and submit for exsiting content type

hey i got a contnet type that i made and i want to twweak it some , when i do form alter its all good but once i do submit its ignore my changes that i added a field for example, how can i do that changes and tell him to insert also that field? do i need to put all the fields again in hook_submit?
$function YOURMODULE_form_alter(&$form, &$form_state) {
//...
$form['#submit'][] = 'YOURMODULE_submitfunction';
//...
}
function YOURMODULE_submitfunction($form, &$form_state) {
// Save your own changes here to DB or something other
}

How can I get the title of a form element in Drupal?

For example, in the registration form, there is "Username" and the text field for it which has the input type="text" name="name" ....
I need to know how can I get the title from the input field's name.
I'm expecting a function like:
$title = get_title_for_element('name');
Result:
assert($title == 'Username'); // is true
Is there something like this in Drupal?
Thanks.
You have the form and the form state variables available to your validation function. You should use form_set_error() to set the error.
There is no function that I am aware of which will map from the values array to the form array. But it is not dificult to work it out. Understanding the form data structure is one of the key skills you need when building drupal.
In this case the form in question is generated (in a roundabout way) by user_edit_form, you can see the data structure in there.
$form['account']['name'] is the username field. and the array key for the title is '#title' as it will be in most cases for form elements.
You can do it in two different ways as I see it. Let's create a module called mycustomvalidation.module (remember to create the mycustomvalidation.info file also).
Note: The code below has not been tested, so you might have to do some minor adjustments. This is Drupal 6.x code by the way.
1) Using hook_user()
What you need is a custom module containing your own implementation of hook_user() http://api.drupal.org/api/function/hook_user/6.
<?php
function mycustomvalidation_user($op, &$edit, &$account, $category = NULL) {
if ($op == 'validate') {
// Checking for an empty 'profile_fullname' field here, but you should adjust it to your needs.
if ($edit['profile_fullname'] != '') {
form_set_error('profile_fullname', t("Field 'Fullname' must not be empty."));
}
}
}
?>
2) Using form_alter() and a custom validation function
Personally, I would go for this option because I find it cleaner and more "correct". We're adding a custom validation function to our profile field here.
<?php
function mycustomvalidation_form_alter(&$form, $form_state, $form_id) {
// Check if we are loading 'user_register' or 'user_edit' forms.
if ($form_id == 'user_register' || $form_id == 'user_edit') {
// Add a custom validation function to the element.
$form['User information']['profile_fullname']['#element_validate'] = array('mycustomvalidation_profile_fullname_validate');
}
}
function mycustomvalidation_profile_fullname_validate($field) {
// Checking for an empty 'profile_fullname' field here, but you should adjust it to your needs.
if ($field['#value'] != '') {
form_set_error('profile_fullname', t("Field %title must not be empty.", array('%title' => $field['#title']));
}
}
?>

Resources