0

For single instances of each field, I've successfully followed arajashe's excellent answer for controlling what input fields get displayed in the content type's create/edit form here: https://drupal.stackexchange.com/a/68975/1082 - an answer to question: How do you theme a content type's create/edit form in Drupal 7?

But what about fields where you can add more, where you specify in the settings that more than 1 of a field can be added, or even unlimited.

In this case, the above answer does not work. When I click the 'add more items' button for such fields, in my custom form, the new field does not display. Given that I have already displayed the first instance of the field, I would expect the markup output to include a place holder for contain the new input field that would be inserted as a result of clicking the button.

I've started to debug the code involved - 2 screenshots

The first is in field.form.inc and shows the ajax call back code - the code that is called when the add more items button is pressed. This does the job of inserting the markup for the new instance of the field input. For some reason (which is why I'm asking this question), the markup for the second field is not being inserted alongside the first field by this function, when I use a custom template.

ajax call back function to insert new instance of field

The second screenshot shows 2 fields of the same type and the markup that generates them. This is when no custom template is being used - i.e. the default, out-of-the-box input form for add content type that comes with Drupal.

the field shown on the form and the markup placeholder containing it

1

The successful answer I found would be not to build the form from scratch as described in: https://drupal.stackexchange.com/a/68975/1082

But instead to use the standard default content type entry form and to customise it to only show certain fields, using the php unset function to hide the input fields that you don't want to be displayed (provided they are non-mandatory of course).

In theme code, template.php

function theme_form_alter(&$form, &$form_state, $form_id) 
{
  if($form_id == 'machine_name_of_your_form') 
  {
    unset( $form['field_email'] );
  }
}

The benefit of this approach is that you start with a working form and take things off that you don't need.

Contrast that with building a custom form from scratch which doesn't have that guarantee of working - well I found this anyway - as it seemed that even though here I included the essentials, things like multiple field handling didn't work and the submit didn't work either.

So starting with the complete form and tweaking worked for me.

Update

An approach better than unset (and in fact is the official Drupal approach), is to use the #access property on form fields to hide them. For example, in your-theme_hook_alter function (hook_form_alter) in your theme's template.php you would do:

$form['field_email']['#access'] = false;

More details here: http://themery.com/dgd7/advanced-theming/forms/with-templates (which is an excerpt from THE DEFINITIVE GUIDE TO DRUPAL 7 ebook and paper-based physical book from apress - see http://dgd7.org/ and http://apress.com)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.