extend the Mytiny todo module in drupal7 - php

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.

Related

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.');
}
}
?>

Theming Drupal Add Block form via forms API

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,
);
}
}

Zend Framework Form Irrational Behaviour

Let's start this off with a short code snippet I will use to demonstrate my opinion:
$title = new Zend_Form_Element_Text('title', array(
'label' => 'Title',
'required' => false,
'filters' => array(
'StringTrim',
'HtmlEntities'
),
'validators' => array(
array('StringLength', false, array(3, 100))
),
));
This important line is:
'required' => false,
Which means that the input field is not required and you can submit the form without filling it. However, this also means that any filters and validators won't apply to it if you choose to fill in this field.
Common sense tells me that is an irrational behavior. The way I understand the word 'required' in relation with HTML input fields: an input field that is not required should return NULL if it is not filled in but if user decides to fill it both filters and validators should apply to it. That's what makes sense to me. Do you agree with me or is my common sense not so common?
Now more practical question, because this is how Zend_Form behaves, how can I achieve not required fields which would work as I described above (if nothing is typed in by user it returns NULL otherwise filters and validators normally apply).
Not really a complete answer to your question, but since comments don't have syntax formatting; here's a filter you can use to make your field values null if empty.
class My_Filter_NullIfEmpty implements Zend_Filter_Interface
{
public function filter( $value )
{
// maybe you need to expand the conditions here
if( 0 == strlen( $value ) )
{
return null;
}
return $value;
}
}
About the required part:
I'm not sure really. You could try to search the ZF mailinglists on Nabble:
http://www.nabble.com/Zend-Framework-Community-f16154.html
Or subscribe to their mailinglist, and ask them the question. Either through Nabble, or directly via the addresses on framework.zend.com:
http://tinyurl.com/y4f9lz
Edit:
Ok, so now I've done some tests myself, cause what you said all sounded counter intuitive to me. Your example works fine with me. This is what I've used:
<?php
class Form extends Zend_Form
{
public function init()
{
$title = new Zend_Form_Element_Text('title', array(
'label' => 'Title',
'required' => false,
'filters' => array(
'StringTrim',
'HtmlEntities',
'NullIfEmpty' // be sure this one is available
),
'validators' => array(
array('StringLength', false, array(3, 100))
),
));
$this->addElement( $title );
}
}
$form = new Form();
$postValues = array( 'title' => '' ); // or
$postValues = array( 'title' => ' ' ); // or
$postValues = array( 'title' => 'ab' ); // or
$postValues = array( 'title' => ' ab ' ); // or
$postValues = array( 'title' => '<abc>' ); // all work perfectly fine with me
// validate the form (which automatically sets the values in the form object)
if( $form->isValid( $postValues ) )
{
// retrieve the relevant value
var_dump( $form->getValue( 'title' ) );
}
else
{
echo 'form invalid';
}
?>
Actually, what you describe as your expectations are exactly how Zend_Form works. If you mark the element as not required, then the following happens: (a) if no value is passed, it skips validation, but if (b) a value is passed, then it must pass all validators in order to be valid.
BTW, best place to ask ZF questions is on the ZF mailing lists: http://framework.zend.com/archives

Categories