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.
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'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 ?
I'm working on an e-commerce project and I got stuck at the cart update. Here I have to present a form using the contents of the current cart, with input fields containing the current quantities.
I checked the documentation and the forums, but I didn't find anything useful. The problem is that i cannot declare the exact form fields in my form class because I don't know how many fields will be there. I tried this:
class CartForm extends sfForm {
public function configure()
{
$cart = sfContext::getInstance()->getUser()->getShoppingCart();
foreach ($cart->getItems() as $item) {
$widgetName = $item->getId().'_quantity';
$this->widgetSchema[$widgetName] = new sfWidgetFormInput(
array(),
array(
'class' => 'quantity-input',
'id' => null,
'name' => $widgetName
)
);
$this->widgetSchema->setDefault($widgetName, $item->getQuantity());
$this->validatorSchema[$widgetName] = new sfValidatorInteger(array(
'required' => true,
'min' => 1
),
array());
}
unset($cart);
$this->getWidgetSchema()->getFormFormatter()->setRowFormat('%field%%error%%hidden_fields%');
}
}
but I got some errors:
Fatal error: Cannot use object of type sfShoppingCart as array in /home/sfprojects/mdmall/lib/vendor/symfony/lib/form/sfForm.class.php on line 784
so this is not the right way. I tried to use raw fields without any form classes (and validators) but something very odd happens, instead of getting the $_POST values i get a 404 error because when I submit the form it doesn't trigger this:
cart_update:
url: /cart/update.:sf_format
class: sfRequestRoute
param: { module: cart, action: update, sf_format: html }
requirements: { sf_method: post }
If I remove the requirement, cart/update runs, but I dont have the $_POST data in the request object. Do you have any ideas?
These will help you with regard to dynamically adding form fields and working with validation of those fields:
Dynamic Emedded forms (but the process is similar for fields)
Dynamic Form Fields
Custom Validation
All I need to do is have a form that does this:
User enters postcode in text box
Upon submit user is redirected to mysite.com/[user postcode]
That's it! I know validation etc. would be desirable as well, but I just need to get this working for now. I don't mind if it's harcoded or utilises the Drupal form API (actually I'd prefer the former!).
I know this is dead simple, but unfortunately I'm coming from a front-end background and have a bit to learn about this sort of thing :(
Cheers!
This is pretty easy with the Form API and a custom module. You'll build a form using the Form API and add a submit handler that changes the redirect for the form to whatever you'd like. Finally, you'll need to create a way to access the form (either by creating a menu item or by creating a block).
Here's an example that implements a form like you want: you'll want to peruse the Form API reference to see all the options you have when building a form. It also provides two ways to access the form:
Using hook_menu() to provide a page for the form at http://example.com/test
Using hook_block() to provide a block containing the form that you can add and move around on the block administration page.
Example code:
// Form builder. Form ID = function name
function test_form($form_state) {
$form['postcode'] = array(
'#type' => 'textfield',
'#title' => t('Postcode'),
'#size' => 10,
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Go'),
);
return $form;
}
// Form submit handler. Default handler is formid_submit()
function test_form_submit($form, &$form_state) {
// Redirect the user to http://example.com/test/<Postcode> upon submit
$form_state['redirect'] = 'test/' . check_plain($form_state['values']['postcode']);
}
// Implementation of hook_menu(): used to create a page for the form
function test_menu() {
// Create a menu item for http://example.com/test that displays the form
$items['test'] = array(
'title' => 'Postcode form',
'page callback' => 'drupal_get_form',
'page arguments' => array('test_form'),
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
// Implementation of hook_block(): used to create a movable block for the form
function test_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list': // Show block info on Site Building -> Blocks
$block['postcode']['info'] = t('Postcode form');
break;
case 'view':
switch ($delta) {
case 'postcode':
$block['subject'] = t('Postcode');
$block['content'] = drupal_get_form('test_form');
break;
}
break;
}
return $block;
}
More info:
Creating modules - a tutorial: Drupal 6.x
Form API Quickstart Guide
Form API Reference
hook_menu() API reference
hook_block() API reference
Example of hook_block in Drupal 6
The Drupal Form API -is- dead simple and its something you need to learn as a developer eventually. Might as well jump in and do it through the API since its not too hard, what you are trying to do.
Creating forms in Drupal is fairly easy once you get the hang of it. I would recommend reading the following link. http://drupal.org/node/751826 It gives a good overview of how to create the form.
In the _submit hook, you can then redirect to the appropriate page by setting $form_state['redirect'].
This is assuming of course that you already have the hang of creating custom modules. If you need more info on that, go here.
OK so this is my hook form alter function.It is causing all the registration forms on site to be over written which I do not want as I just want it on this page.
function special_registration_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'user_register') {
drupal_set_title(t('Custom registration'));
$form['firstname'] = array('#type' => 'textfield',
'#title' => t('First Name: *'),
'#required' => TRUE,
'#size' => 45,
'#weight' => - 100,);
$form['lastname'] = array('#type' => 'textfield',
'#title' => t('Last Name: *'),
'#required' => TRUE,
'#size' => 45,
'#weight' => - 99,);
}
I only first name and last name to be captured and stored in a different table just on this page.
On other pages I just want the good old fashioned form. Do I still need to change the weight? I know I am missing something elementary.
You just need a check for the current page, using either arg or $_GET['q'].
eg:
function special_registration_form_alter(&$form, $form_state, $form_id) {
if ($_GET['q'] !== 'whatever/path' ) { return false; }
..rest of code..
}
If you want to restrict your form alterations to a specific page, you can simply add a check for the page to your form id check, e.g.:
function special_registration_form_alter(&$form, $form_state, $form_id) {
// Alter the registration form, but only on 'user/register' pages
if ($form_id == 'user_register' && 'user' == arg(0) && 'register' == arg(1)) {
// snipped alteration code
}
}
You could make use of the Profile module in the Core list of modules as well. It will solve this without any programming, fyi.
Implement hook_user(); the function allow to alter the form presented to the users when they register on a site.
hook_user() is used by user.module, and it is independent from the profile module.
Defining the hook as hook_user($op, &$edit, &$account, $category = NULL), the parameter $op will contain the value 'register' when the registration form is being presented to the user. In that case, the module returns the form fields it wants to add to the registration form.
If you don't really need to create user accounts, like for a simple event registration. If instead you're collecting just names, you could use the webform module instead.