How to expand a default collapsed link in the edit form on certain nodetypes only?

Rosamunda - May 4, 2009 - 22:02

Hello there!
I would like to expand by default all "add attachment" links in certain nodetypes only.
My guess is that it can be done from template.tpl.php. I´ve searched around and didn´t find exactly what I was looking for, because usually all posts are reffering to specific links created by specific contrib modules.
I want to expand the default attachment link. Let it still be collapsible, but expanded by default on all nodes of type A, B and F only.

Hope you can help me out!
Rosamunda

hook_form_alter

Jody Lynn - May 4, 2009 - 23:15

You need to create a new module and use hook_form_alter to modify the form.

<?php
function YOURMODULE_form_alter(&$form, &$form_state, $form_id) {
  if (
$form_id == 'page_node_form' || $form_id == 'othercontenttype_node_form') {
   
$form['attachments']['#collapsed'] = FALSE;
  }
}
?>

--Zivtech--

template.php

grobemo - May 5, 2009 - 02:53

You can do this from template.php, too. You need to declare a function to theme the form with hook_theme, and then include Jody's code in that function, like so:

<?php

function YOURTHEME_theme($existing, $type, $theme, $path) {
  return array(
   
'YOUR_FORM_NAME' => array(
     
'arguments' => array('form' => NULL),
    ),
  );
}

function
YOURTHEME_preprocess_YOUR_FORM_NAME(&$vars) {
 
$vars['form']['attachments']['#collapsed'] = FALSE;
}

?>

(Note the & in &$vars. Without it, the changes to $vars will be ignored by your template.)

For more on using hook_theme to theme forms, see this tutorial on Trellon or this tutorial on 11 heavens about theming the user register form.

Oh! I didn´t see your reply

Rosamunda - May 5, 2009 - 03:30

Oh! I didn´t see your reply when posted my last message. Sorry :)
Thanks you too grobemo! Personally I prefer to do this from the template.tpl. I think it´s easier than crating a whole new module for just a tiny hook.
Thanks!
Rosamunda

Rosamunda
Buenos Aires | Argentina

You just saved me Jody,

Rosamunda - May 5, 2009 - 03:09

You just saved me Jody, thanks!!!!!!!!
Works like a charm!

Rosamunda

Rosamunda
Buenos Aires | Argentina

Altering an already altered form

talino - August 23, 2009 - 21:21

I've tried this and it didn't work, because I tried to uncollapsed a fieldset already modified by another module (Taxonomy Super Select). A solution I found is detailed here: http://drupal.org/node/549562#comment-1960232.

 
 

Drupal is a registered trademark of Dries Buytaert.