How can I add a custom configuration area to a node edit form just beneath the Authoring Information & Publishing Options section?
You can use hook_form_FORM_ID_alter().
Example below:
function my_module_form_node_form_alter(&$form, $form_state) {
// if you are targeting a specific content type then
// you can access the type:
$type = $form['#node']->type;
// Then
if ($type == 'my_content_type') {
// use a contact settings for the sake of this example
$form['contact'] = array(
'#type' => 'fieldset',
'#title' => t('Contact settings'),
'#weight' => 100,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
// add simple checkbox to the field set
$form['contact']['approve'] = array(
'#type' =>'checkbox',
'#title' => t('Contact me'),
);
}
}
Now For storing the data I encourage you to see the examples project; it has many code examples with lots of documentation.
Also, Check the Form API for more information on different types of form elements.
Hope this helps.
The follow code generates the last menu in the attached image:
$form['barclays_epdq'] = array(
'#type' => 'fieldset',
'#access' => TRUE,
'#title' => 'Barclays ePDQ',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'additional_settings',
'#weight' => 100,
'barclays_epdq_active' => array(
'#type' => 'checkbox',
'#title' => 'Enable this webform to send users to your Barclays ePDQ store to make a payment',
'#default_value' => $active_state,
),
);
ps: the form is in hook_form_alter
Related
I am trying to use CakePdf to generate a pdf file within CakePHP. I have a function in the CourseGradesController called viewReport. I want viewReport to allow you to select a student from a select field, and then upon submitting the form, it will generate a PDF with the appropriate data. If I put the data into a table and do not try to make a PDF, the page will display correctly, so I don't think that is the problem. In bootstrap.php, I have
CakePlugin::load('CakePdf', array('bootstrap' => true, 'routes' => true));
Configure::write('CakePdf', array(
'engine' => 'CakePdf.WkHtmlToPdf',
'options' => array(
'print-media-type' => false,
'outline' => true,
'dpi' => 96
),
'margin' => array(
'bottom' => 15,
'left' => 50,
'right' => 30,
'top' => 45
),
'binary' => '/var/www/cakephp/app/Plugin/CakePdf/Vendor/WkHtmlToPdf',
'orientation' => 'landscape',
'download' => false
));
I have the WkHtmlToPdf folder moved into /var/www/cakephp/app/Plugin/CakePdf/Vendor/WkHtmlToPdf, so the lib and include folders are in that directory.
In the viewReport function of CourseGradesController, I have
function viewReport($id = null)
{
$this->CourseGrade->id = $id;
if (!$this->CourseGrade->exists())
{
throw new NotFoundException(__('Invalid invoice'));
}
$this->pdfConfig = array(
'orientation' => 'portrait',
'filename' => 'Invoice_' . $id
);
$this->set('invoice', $this->CourseGrade->read(null, $id));
...
}
If I navigate to /courseGrades/viewReport, I get the error "Error: The requested address '/cakephp/courseGrades/viewReport' was not found on this server."
If I naviate to /courseGrades/viewReport/1.pdf, then I just see a completely blank screen.
I would strongly recommend the use of this plugin for this kind of work.
https://github.com/ceeram/CakePdf
Hi being a newbie to drupal , kindly guide how to add a new user , i know user module with user_save can be used but how and where to implement it ?
Do i have to write this function in submit handler of my custom form below-
function registerme_newform($form, &$form_state)
{
$form = array();
$form['account details'] = array(
'#type' => 'fieldset',
'#title' => t('Account Details'),
'#description' => t('Enter Personal Credentials'),
);
$form['account details']['first name'] = array(
'#type' => 'textfield',
'#title' => t('First Name'),
'#default_value' => t('Be sure of your first name'),
'#attributes' => array(
'onblur' => "if (this.value == '') {this.value = 'Be sure of your first name'}",
'onfocus' => "if (this.value == 'Be sure of your first name') {this.value = ''}"
, ),
);
$form['account details']['last name'] = array(
'#type' => 'textfield',
'#title' => t('Last Name'),
'#default_value' => t('Be sure of your last name'),
'#attributes' => array(
'onblur' => "if (this.value == '') {this.value = 'Be sure of your last name'}",
'onfocus' => "if (this.value == 'Be sure of your last name') {this.value = ''}"
, ), );
$form['account details']['email'] = array(
'#type' => 'textfield',
'#title' => t('E-Mail'),
'#required' => TRUE,
'#default_value' => t('Email acts as username.Dont forget it!'),
'#attributes' => array(
'onblur' => "if (this.value == '') {this.value = 'Email acts as username.Dont forget
it!'}",
'onfocus' => "if (this.value == 'Email acts as username.Dont forget it!')
{this.value = ''}"
, ),
);
$form['account details']['password'] = array(
'#type' => 'password',
'#title' => t('Password'),
'#maxlength' => 60,
'#size' => 60,
'#required' => TRUE,
'#description' => t('Password should be atleast 6 characters long.'),
);
$form['home'] = array(
'#type' => 'fieldset',
'#title' => 'Home Address',
'#description' => t('Enter Personal Residential Credentials')
);
$form['home']['street'] = array(
'#type' => 'textfield',
'#title' => 'Street Address',
);
$form['home']['city'] = array(
'#type' => 'textfield',
'#title' => 'City',
);
$form['work'] = array(
'#type' => 'fieldset',
'#title' => 'Work Address',
'#description' => t('Enter Official Address')
);
$form['work']['street'] = array(
'#type' => 'textfield',
'#title' => 'Street Address',
);
$form['work']['city'] = array(
'#type' => 'textfield',
'#title' => 'City',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Register Me'),
);
return $form;
}
function registerme_newform_validate(&$form, &$form_state)
{
if($form_state['values']['account details']['first name']=='Be sure of your first
name')
{
form_set_error('first name',t('Please enter your first name'));
}
}
function registerme_newform_submit(&$form, &$form_state)
{
dsm($form_state);
}
Also would like to know how will values be entered in database , i mean how will these custom fields be added to database? Kindly guide thanks.
First of all please check if the first name is allowed user name. I recommend you providing another field to enter username as its not uncommon that username is already used by somebody else. Then also check that there are no users registered with the given email address.
function registerme_newform_validate(&$form, &$form_state) {
if($form_state['values']['account details']['first name']=='Be sure of your first
name') {
form_set_error('first name',t('Please enter your first name'));
}
// TODO: Provide user with a chance to enter a name ( drupal user name )
// Also do the following two checks and report an error.
// check using user_load_by_name($form_state['values']['account details']['first anme'])
// check using user_load_by_mail($form_state['values']['account details']['email'])
}
Next in your submit function map the form values to an array that could be passed to user_save as shown bellow. If you want to know exactly how other fields map, use the devel module to inspect the structure by vising an already existing user profile.
function registerme_newform_submit(&$form, &$form_state){
$new_user = array(
// The below name field must be unique.
'name' => $form_state['values']['account details']['name'],
'pass' => $form_state['values']['account details']['password'],
'mail' => $form_state['values']['account details']['email'],
'init' => $form_state['values']['account details']['email'],
'field_first_name' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['account details']['first name']))),
'field_last_name' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['account details']['last name']))),
// Map all the extra fields to user objects as shown above.
...
'status' => 1,
'access' => REQUEST_TIME,
'roles' => $roles,
);
// $account returns user object
$account = user_save(null, $new_user);
//after user_save you can use the following code to send notification email.
drupal_mail('user', 'register_no_approval_required', $account->mail, NULL, array('account' => $account), variable_get('site_mail', 'noreply#mysite.com'));
}
I have implemented a different approach to this problem. I created an entity form (https://drupal.org/project/entityform)
where I prompt for key user information. I have also installed the profile2 module allowing me to create alternate profiles. When the form is submitted it triggers a rule. That rule allows me to create the user, create a linked profile and then send an email to the administrator with a link to the new user entry allowing them to review and activate.
A nice alternative if you dont want to solve via PHP or want to create additional content on creation.
For some reason my allowed_values_function never gets called when showing a field on a user bundle. Code:
function get_business_units()
{
$options = entity_load('business_unit', FALSE, NULL, FALSE);
$opt = bu_to_list_values($options);
return $opt;
}
function MYMODULE_enable()
{
if (!field_info_field('field_user_business_unit')) {
$field = array(
'field_name' => 'field_user_business_unit',
'type' => 'text',
'settings' => array(
'allowed_values' => array(),
'allowed_values_function' => 'get_business_units',
)
);
field_create_field($field);
// Create the instance on the bundle.
$instance = array(
'field_name' => 'field_user_business_unit',
'entity_type' => 'user',
'label' => 'Business Unit',
'bundle' => 'user',
'required' => FALSE,
'settings' => array(
'user_register_form' => 1,
),
'widget' => array(
'type' => 'options_select',
),
);
field_create_instance($instance);
}
}
The field is created, and even displayed on the users "edit" page when editing their info. But the only value is "Select" or "None". My method is never called (I even placed a debug point). This is all in MYMODULE.install file.
The problem is: 'type' => 'text'.
You have to use: 'type' => 'list_text'.
Allowed values is meaningless for a text type.
Your get_business_units() function needs to be in the MYMODULE.module file; the .install files aren't included in a normal Drupal bootstrap.
Have you tried
drush features-revert MYMODULE ?
I am trying to put together a module which will have it's own configuration page. For that purpose, I am using the following code as my menu page-callback:
function webform_customizations_customize() {
$form = array();
// Text field for the e-mail subject.
$form['webform_customizations']['user_warn_e-mail_subject'] = array(
'#type' => 'textfield',
'#title' => t('Warning e-mail subject'),
'#description' => t('The subject of the e-mail which will be sent
to users.'),
'#size' => 40,
'#maxlength' => 120,
'#required' => TRUE,
);
$options = array('A', 'B', 'C');
$form['test'] = array(
'#type' => 'radios',
'#options' => array(
'one' => t('one'),
'two' => t('two'),
),
'#title' => t('roles that should see a minimal webforms setting area'),
'#title_display' => t('testing'),
);
$form['high_school']['tests_taken'] = array(
'#type' => 'checkboxes',
'#options' => drupal_map_assoc(array(t('SAT'), t('ACT'))),
'#title' => t('What standardized tests did you take?'),
'#states' => array(
'visible' => array(// action to take.
':input[name="student_type"]' => array('value' => 'high_school'),
),
),
);
return $form;
}
My problem is my attempts to display checkboxes are failing. The first field shows a textfield successfully. But the next two are checkboxes which never show on my configuration page - and the tests_taken checkbox code is lifted directly from this drupal doc page without any amendments.
I tried a single checkbox and that works.
You should have read the comments that were along with the code you took:
// This #states rule says that this checkboxes array will be visible only
// when $form['student_type'] is set to t('High School').
// It uses the jQuery selector :input[name=student_type] to choose the
// element which triggers the behavior, and then defines the "High School"
// value as the one that triggers visibility.
Simply remove the #states element, and it should work.
Happy drupaling!
I am attempting to create a node/content type in drupal, accordingly I have a .info, .install and .module file at minimum.
The module is created fine and I am able to enable/disable it from the module administration page, also, Drupal is able to recognize this module as a content type and it appears when I click 'Add content' in the Content menu.
Everything works fine, but it does not show the form elements, rather it starts directly at
The form element code is listed below:
function newNode_form($node,&$form_state)
{
$type = node_get_types('type',$node);
$form['title']= array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#default_value' => !empty($node->title) ? $node->title : '',
'#required' => TRUE,
'#weight' => -5,
);
$form['field1'] = array(
'#type' => 'textfield',
'#title' => t('Custom field'),
'#default_value' => $node->field1,
'#maxlength' => 127,
);
$form['selectbox'] = array(
'#type' => 'select',
'#title' => t('Select box'),
'#default_value' => $node->selectbox,
'#options' => array(
1 => 'Option A',
2 => 'Option B',
3 => 'Option C',
),
'#description' => t('Choose an option.'),
);
return $form;
}
Can anybody tell me what's wrong
P.S: Just F.Y.I: In my .install file, there exists only install and uninstall hook functions. I have yet to create DB tables, this content type is a walkthrough for me to create content type UI and not necessarily a full blown UI.
Drupal's hook system uses lower cases and under scores to dynamically load module functions.
<module name>_<hook_name>
Try declaring your function like this:
function new_node_form($node, &$form_state) {
...