i'm using Drupal 6.19 , in order to do some changes to the forms i followed some tutorials on the web using the form_alter hook .
well identifying the form consist of testing $form_id (that worked fine) ,but adding new fields is done by manipulating the $form variable .
when i tried this on my code it didn't work ,so i tried to manipulate the $form_id variable instead and it worked !
i know that my problem is solved but i want to know what the difference between $form_id and $form ?
isn't $form_id suppose to store only the form identifier ? and the form content goes into $form ?
Since you're using Drupal 6 , you are using the wrong syntax for the hook_form_alter. What you have is the Drupal 5 syntax for the hook. This is what it should be...
hook_form_alter(&$form, &$form_state, $form_id)
Because you're using it the way you are, the $form_id variable IS actually the $form variable. Try swapping out to the correct one, and that'll help you out.
Here's a link to the documentation: Drupal API: hook_form_alter
ok, here is my code for the form_alter hook :
btw, i use dBug to view the content of variables (that's how i figured out that $form does'nt contain the form structure)
function testymodule_form_alter($form_id, &$form) {
include_once("dBug.php");
if ($form_id['#id']=='node-form') {
$form_id['testymodule_checkbox'] = array(
'#type' => 'checkbox',
'#title' => t('Newly testy Checkbox'),
);
new dBug($form_id);
}
this will add a new checkbox to the mentioned form (although it maniuplate $form_id not $form)
what i found on the net was manipulating $form :
function testymodule_form_alter($form_id, &$form) {
include_once("dBug.php");
if ($form_id['#id']=='node-form') {
$form['testymodule_checkbox'] = array( //here is the clue
'#type' => 'checkbox',
'#title' => t('Newly testy Checkbox'),
);
new dBug($form_id);
}
it's weird nan ?
Related
I am using Drupal 8.2.6 and I would like to create a block that would appear on a custom content type page.
It is kind of a booking block which sends an e-mail to the site admin that a visitor would like to book a product (the custom content type).
I assume I would need a form which only consists of a submit button and a block which renders the form.
But the real point would be sending the mail with the product's reference to the site admin.
As I found here, I could get the values I need using this snippet:
if ($node = \Drupal::routeMatch()->getParameter('node')) {
$field_my_custom_value = $node->field_my_custom_value->value;
}
But I am not sure in which scope of my code I should use it. This example was for rendering the values within a custom block, where as my case would be sending a mail with the values.
And could anyone remind me as well how to send a mail from a custom module in Drupal 8?
Thanks a lot
So, after solving it myself after a day's whole worth of documentation, here are the solutions, as I am going to revert back my question to earlier revisions, in case anyone needs it.
So, given the snippet in the question above, I declared the variables in the buildForm() function
public function buildForm(array $form, FormStateInterface $form_state) {
$field_value = '';
if ($node = \Drupal::routeMatch()->getParameter('node')) {
$field_value = $node->field_name->value;
}
$form['field_value'] = array(
'#type' => 'value',
'#value' => $field_value,
);
// And then you add the definition for other form items and submit button
}
And for sending the mail using the value, you retrieve the value using $form_state like this:
public function submitForm(array &$form, FormStateInterface $form_state) {
$module = 'your_module_name';
$key = 'any_key_you_would_like';
$to = 'receiver#email.address';
$langcode = 'en';
$params = array(
'body' => 'Node is booked',
'subject' => $form_state->getValue('field_value'),
);
$mailer = \Drupal::service('plugin.manager.mail');
$mailer->mail($module, $key, $to, $langcode, $params, NULL, TRUE);
}
Some values are quite tricky to get from the node, such as the node title which uses $node->getTitle() instead of $node->field_name->value so you would like to check your fields using Drupal 8's Devel + Kint module to know what attributes and methods to use.
I am trying to learn drupal so right now my website resides on my localhost. I am using DRUPAL 7. I have created a contact us page in my drupal site using drupal's contact module. I want to add one more fields(Phone Number) to the existing contact form and need to sent the value along with the email. How is it possible to do something like this.
I have used the following code in my contact module page
function mymodulename_form_contact_site_form_alter(&$form, &$form_state) {
$form['phone'] = array(
'#title' => t('Phone'),
'#type' => 'textfield',
'#required' => TRUE,
);
$order = array(
'name',
'mail',
'phone',
'subject',
'cid',
'message',
'copy',
'submit'
);
foreach ($order as $key => $field) {
// Set/Reset the field's
// weight to the array key value
// from our order array.
$form[$field]['#weight'] = $key;
}
}
But the field is not showing up on the website page. Please help.
Thanks
Now its all fine. I had placed the code in the existing contact module file. That was the issue. Now I create a custom module and placed the similar code there. Now its all fine.
Only change needed to make it working is instead of using this
function mymodulename_form_contact_site_form_alter(&$form, &$form_state) {
use the below line
function mymodulename_form_contact_site_form_alter(&$form, &$form_state, &$form_id) {
Hope this helps someone.
The Zend Form is proving to be a bit tricky for me, even as much as I am working with it lately...
I have this form and I am attempting to dynamically create the several checkboxes for it. It is working out alright, except I cannot seem to get the 'value' attribute to change.
In my Zend form class I have this snippet...
// psychotic symptoms
$this->addElement('checkbox', 'psychoticsymptom', array(
'label' => 'psychoticsymptom',
'name' => 'psychoticsymptom',
));
In my view (phtml) I am calling it like this...
<div class="element">
<?php // Psychotic Symptoms
$Criteria = new Criteria();
$Criteria->add( DictionaryPeer::CATEGORY, 'MAR: Psychotic Symptoms' );
$Criteria->addAscendingOrderByColumn( 'Ordinal' );
$this->PsychoticSymptomsList = DictionaryPeer::doSelect( $Criteria );
foreach( $this->PsychoticSymptomsList as $Symptom ) {
$form->psychoticsymptom->setValue($Symptom->getDictionaryId());
$form->psychoticsymptom->setAttrib('name', $Symptom->getWord());
echo $Symptom->getDictionaryId(); // prove my id is coming through... (it is)
$form->psychoticsymptom->getDecorator('label')->setTag(null);
echo $form->psychoticsymptom->renderViewHelper();
$form->psychoticsymptom->setLabel($Symptom->getWord());
echo $form->psychoticsymptom->renderLabel();
echo '<br />';
}
?>
</div>
Everything seems to be working fine, except the value attribute on each checkbox is rendering a value of '1'. I have tried moving the 'setValue' line to several different positions, as to set the value before the form element renders but I am having no luck getting this to work. It's worth any effort to me because I need to do the same type of operation in many areas of my application. I would have done this a bit different too, but I am re-factoring another application and am trying to keep some things unchanged (like the database for instance).
Any help is much appriciated
Thanks
you can try to overwrite the "checkedValue" and "uncheckedValue". check this reference
$this->addElement('checkbox', 'psychoticsymptom', array(
'label' => 'psychoticsymptom',
'name' => 'psychoticsymptom',
'checkedValue' => 'checked Value',
'uncheckedValue' => 'unchecked Value'
));
You seem to only have one psychoticsymptom element "checkbox" which your adding (changing) the value too for each $this->PsychoticSymptomsList.
Maybe you would be better off using a multicheckbox element instead.
$actions = array(
'EDIT' => sprintf('Edit',
'abf_cm',
'edit_course',
$item['course_id'],
'thickbox edit-box',
'edit_'.$item['course_id']
),
'DELETE' => sprintf('Delete','course_management','do_process','delete',$item['course_id']),
);
In doing so, the edit part is not being displayed.Am i doing anything wrong.
I also tried using the placeholders
'EDIT' => sprintf('Edit',
'abf_cm',
'edit_course',
$item['course_id'],
'thickbox edit-box',
'edit_'.$item['course_id']
),
but still no results. I also noticed that when i remove the class and id attributes in the earlier version, then it works fine.
Can you please give me a satisfactory explanation of this and tell me where am i doing wrong.
EDIT:
Im using this inside Wordpress for creating custom table using WP_List_Table class
function column_course_name($item ) {
//Build row actions
$actions = array(
'EDIT' => sprintf('Edit',
'abf_cm',
'edit_course',
$item['course_id'],
'thickbox edit-box',
'edit_'.$item['course_id']
),
'DELETE' => sprintf('Delete','book_management','do_process','delete',$item['course_id']),
);
//Return the title contents
return sprintf('%1$s%3$s',
/*$1%s*/ strlen($item['course_name'])>0?$item['course_name']:'<span style="color:silver">(No Name)</span>',
/*$2%s*/ $item['course_id'],
/*$3%s*/ $this->row_actions($actions) //row_actions is a method in this class
);
}
update:
Well, its strange to mention but the code works when i use a single class( ie when i delete the space between the two classes for the tag) .
Any thoughts?
Dipesh, maybe you have errors in the code around this snippet.
Try to check your code in isolation. I copied your code to the separate .php script with little set-up and checked $actions array with print_r, like this:
edit_array.php
<?php
$item = array();
$item['course_id'] = 1;
$actions = array(
'EDIT' => sprintf('Edit',
'abf_cm',
'edit_course',
$item['course_id'],
'thickbox edit-box',
'edit_'.$item['course_id']
),
'DELETE' => sprintf('Delete','course_management','do_process','delete',$item['course_id']),
);
print_r($actions);
I ran this script from console and got the following results:
$ php edit_array.php
Array
(
[EDIT] => Edit
[DELETE] => Delete
)
Generated link for $actions['EDIT'] is HTML valid, so one can safely conclude that your code itself is working fine, and error lies somewhere else.
I am modifying an already contributed drupal module (Inline Ajax Search) to handle searching of a specific content type with some search filters (i.e. when searching for help documentation, you filter out your search results by selecting for which product and version of the product you want help with).
I have modified the module some what to handle all the search filters.
I also added in similar functionality from the standard core search module to handle the presenting of the search form and search results on the actual search page ( not the block form ).
The problem is that when i submit the form, i discovered that I'd lose all my post data on that submit because somewhere, and i don't know where, drupal is either redirecting me or something else is happening that is causing me to lose everything in the $_POST array.
here's the hook_menu() implementation:
<?php
function inline_ajax_search_menu() {
$items = array();
$items['search/inline_ajax_search'] = array(
'title' => t('Learning Center Search'),
'description' => t(''),
'page callback' => 'inline_ajax_search_view',
'access arguments' => array('search with inline_ajax_search'),
'type' => MENU_LOCAL_TASK,
'file' => 'inline_ajax_search.pages.inc',
);
}
?>
the page callback is defined as such (very similar to the core search module's search_view function):
<?php
function inline_ajax_search_view() {
drupal_add_css(drupal_get_path('module', 'inline_ajax_search') . '/css/inline_ajax_search.css', 'module', 'all', FALSE );
if (isset($_POST['form_id'])) {
$keys = $_POST['keys'];
// Only perform search if there is non-whitespace search term:
$results = '';
if(trim($keys)) {
require_once( drupal_get_path( 'module', 'inline_ajax_search' ) . '/includes/inline_ajax_search.inc' );
// Collect the search results:
$results = _inline_ajax_search($keys, inline_ajax_search_get_filters(), "page" );
if ($results) {
$results = theme('box', t('Search results'), $results);
}
else {
$results = theme('box', t('Your search yielded no results'), inline_ajax_search_help('inline_ajax_search#noresults', drupal_help_arg()));
}
}
// Construct the search form.
$output = drupal_get_form('inline_ajax_search_search_form', inline_ajax_search_build_filters( variable_get( 'inline_ajax_search_filters', array() ) ) );
$output .= $results;
return $output;
}
return drupal_get_form('inline_ajax_search_search_form', inline_ajax_search_build_filters( variable_get( 'inline_ajax_search_filters', array() ) ) );
}
?>
from my understanding, things should work like this: A user goes to www.mysite.com/search/inline_ajax_search and drupal will process the path given in my url and provide me with a page that holds the themed form for my search module. When i submit the form, whose action is the same url (www.mysite.com/search/inline_ajax_search), then we go thru the same function calls, but we now have data in the $_POST array and one of them is indeed $_POST['form_id'] which is the name of the form "inline_ajax_search_search_form". so we should be able to enter into that if block and put out the search results.
but that's not what happens...somewhere from when i submit the form and get my results and theme it all up, i get redirected some how and lose all my post data.
if anybody can help me, it'd make me so happy lol.
drupal_get_form actually wipes out the $_POST array and so that's why I lose all my post data.
according to this: http://drupal.org/node/748830 $_POST should really be ignored when doing things in drupal. It's better to find a way around using it. One way is the way described in the link, making ur form data persist using the $_SESSION array. I'm sure there are various other and better ways to do this, but yeah, drupal_get_form was the culprit here...