Theming Drupal Add Block form via forms API - php

Can anyone advise me on customising the Add Block form? (/admin/build/block/add)
I want to hide the "User specific visibility settings" and "Role specific visibility settings" from users. This is what i've got so far, but obviously it's not right and I can't figure out what the array is. Anyone got the experience on this?
function theme_add_block_form($form) {
$form['roles']['#prefix'] = '<div class="hidden">';
$form['roles']['#suffix'] = '</div>';
return drupal_render($form);
}
Thanks,
H
EDIT - perhaps I wasn't clear - I'm comforable using the various form hooks from the API, but my problem in this case is that I can't identify the array elements to use in my function. The devel module doesn't seem to act on the blocks page, and the themer popup block thing is less than clear.

In modules/block/block.admin.inc, function block_admin_configure:
$form['user_vis_settings'] = array(
'#type' => 'fieldset',
'#title' => t('User specific visibility settings'),
'#collapsible' => TRUE,
);
(...)
$form['role_vis_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Role specific visibility settings'),
'#collapsible' => TRUE,
);
Just try to hide $form['user_vis_settings'] and $form['role_vis_settings'].
EDIT:
Don't touch modules/block/block.admin.inc!! (I only was pointing where I found the form fields' names ). Hide the fields in your theme_add_block_form. Instead of wrapping the fields inside a div, you can write $form['user_vis_settings']['#access'] = false;

http://api.drupal.org/api/function/hook_form_alter/6

This is the way to go. Using the http://api.drupal.org/api/function/hook_form_alter/6 as say in an other answer. You need to write this code in a costum module.
<?php
function module_name_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'block_admin_configure') {
$form['user_vis_settings'] = array(
'#type' => 'fieldset',
'#title' => t('User specific visibility settings'),
'#collapsible' => TRUE,
'#access' = FALSE,
);
$form['role_vis_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Role specific visibility settings'),
'#collapsible' => TRUE,
'#access' = FALSE,
);
}
}

Related

Drupal7 - User/Registration Customizing

Please any one help me to how I can do customizing User/Registration page in Drupal7. For ex I have a select option for user group suppose if I choose 'others' option ’ then user is allowed to create new group in separate text box.
I'm new to drupal. Please anyone help me
You can create a custom module and use hook_form_alter.
You can then modify the form by adding drupal form elements.
function custom_module_form_alter(&$form, &$form_state, $form_id) {
if ($form_id === 'user_register_form') {
$form['account']['other'] = array(
'#type' => 'select',
'#title' => t('Other'),
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
);
$form['account']['group'] = array(
'#type' => 'text_format',
'#title' => t('Group'),
'#default_value' => '',
'#format' => NULL,
);
}
}

extend the Mytiny todo module in drupal7

I am trying to extend the Mytiny todo module. Basically, I want to add two more fields like Repeat and url fields with validation. But when I am trying to it with hook_block_view_alter(), the fields are showing outside the block, whereas I want to show it on the task form field. I am trying to this for adding fields:
<?php
function todo_block_view_alter(&$data, $block)
{
dsm($block);
if (is_array($data['content']) && $block->module == 'system') {
$data = array();
$data['content'] ['name'] = array(
'#type' => 'textfield',
'#title' => 'Your Name',
'#size' => 60,
'#required' => TRUE,
);
}
}
If anybody has the answer than please give me the solution as soon as you can.
Thanking You in advance.

How to programmatically add a form to a node in Drupal 7?

I need to add a programmatic form to a node in Drupal 7. How to attach the form to the node?
function addtabexample_form($node, &$form_state) {
$type = node_type_get_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,
);
return $form;
}
You can follow this code sample, using hook_node_view()
function [YOUR_MODULE]_node_view($node, $view_mode, $langcode)
{
$my_form = drupal_get_form('addtabexample_form', $node);
$node->content['my_form_attached'] = array(
'#markup' => drupal_render($my_form),
'#weight' => 10,
);
}
Your code has some problems that would require some rewriting...
First, I'd suggest reading Form API Quickstart, which is a decent source to get the job done.
I'm not sure how you get the $node object to this. You have $node in the function parameters and $form as a return value...
See http://drupal.org/node/197122 for an example (I added the D7 part) of a form that could be embeded in a node.
But doing that is ultra bad - you will face function redeclare problems, indexing problems, and a whole lot of troubles.
I know this is not an actual answer but I don't know how to write this in 500 chars.

How do I use hook_form to create a frontend node form?

I have created a custom module and have used 'hook_form' to create a custom form. I have then created a new page using menu callback and inserted this form on it
This form basically is to create nodes from the frontend. I have all the same fields as in the backend.
Form code:
function input_simple_form($form, &$form_submit) {
$form['title'] = array(
'#type' => 'textfield',
'#default_value' => '',
'#required' => TRUE,
'#title' => 'Title',
'#maxlength' => 1024,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
How do I save this, or how do I tell the hook_form that this is for a specific content type? I know how to use node_save but not sure how I would implement this with the hook_form.
How do I go about achieving what I want?
Thanks
Robert
Use in your callback function:
$my_form = drupal_get_form('input_simple_form');
echo $my_form;
if your are using hook_theme to theme your page parce $my_form as variable and print it in your template/theme file.

Validating an input in a Drupal form

I have a Drupal form wherein someone inputs information, and I need to do a database query to check if it is valid before submitting. I would like to either have a button the user can click to check the validity (or have it be done automatically after the user leaves that field), and then display some information about his selection.
I know I can use hook_form_submit to review a form when it is submitted and then stop the process if there are any errors, but I would like the user to be able to confirm they have selected the correct thing before submitting the form.
I haven't personally tried this module, but it might be what you're looking for:
http://drupal.org/project/ajax
If you're just looking for a way to do real-time lookups (e.g. entering the book barcode and getting the title), you can also use Drupal's autocomplete feature, but it will require you to write your own autocomplete function to handle the database lookups.
Take a look at: Basic form with validate handler. You really just need to add a function similar to mymodule_myform_validate($form, &$form_state) { ... }. From the linked page:
"This adds a new form field and a way to validate it with a validate function, also referred to as a validate handler."
<?php
function my_module_menu() {
$items = array();
$items['my_module/form'] = array(
'title' => t('My form'),
'page callback' => 'my_module_form',
'access arguments' => array('access content'),
'description' => t('My form'),
'type' => MENU_CALLBACK,
);
return $items;
}
function my_module_form() {
return drupal_get_form('my_module_my_form');
}
function my_module_my_form($form_state) {
$form['name'] = array(
'#type' => 'fieldset',
'#title' => t('Name'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['name']['first'] = array(
'#type' => 'textfield',
'#title' => t('First name'),
'#required' => TRUE,
'#default_value' => "First name",
'#description' => "Please enter your first name.",
'#size' => 20,
'#maxlength' => 20,
);
$form['name']['last'] = array(
'#type' => 'textfield',
'#title' => t('Last name'),
'#required' => TRUE,
);
// New form field added to permit entry of year of birth.
// The data entered into this field will be validated with
// the default validation function.
$form['year_of_birth'] = array(
'#type' => 'textfield',
'#title' => "Year of birth",
'#description' => 'Format is "YYYY"',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
// This adds a handler/function to validate the data entered into the
// "year of birth" field to make sure it's between the values of 1900
// and 2000. If not, it displays an error. The value report is // $form_state['values'] (see http://drupal.org/node/144132#form-state).
//
// Notice the name of the function. It is simply the name of the form
// followed by '_validate'. This is the default validation function.
function my_module_my_form_validate($form, &$form_state) {
$year_of_birth = $form_state['values']['year_of_birth'];
if ($year_of_birth && ($year_of_birth < 1900 || $year_of_birth > 2000)) {
form_set_error('year_of_birth', 'Enter a year between 1900 and 2000.');
}
}
?>

Categories