2

In Drupal 8, the first parameter of FormInterface::buildForm() is $form.

When implementing a form builder, one has the option of either adding stuff to this existing array and returning it, or creating a new $form array, discarding what was passed into the function. In my experience, both of these options usually work fine.

What's the point? Is there ever a case when this parameter is needed?

I think to vaguely remember that this is somehow relevant when a form is rebuilt, so this would be the old form.. but I would not really know what to do with it.

  • Is there ever a case when this parameter is needed? How do you create or alter form fields? ;) – No Sssweat Aug 24 '17 at 5:39
  • In formInterface::buildForm() it says, "array $form: An associative array containing the structure of the form." – No Sssweat Aug 24 '17 at 5:47
  • Yeah but this function is responsible for building the form array, usually from scratch. So the return value should be "the structure of the form". Why does it have to be passed in as a parameter? – donquixote Aug 24 '17 at 5:59
  • 1
    Many form builder implementations do not really care about this parameter, they just start with an empty array $form = [], and then add elements. – donquixote Aug 24 '17 at 6:01
  • Looking in $form on a new build, a rebuild and an ajax request, it's always empty, except a class in #attributes for the form id. So this is a very good question. – 4k4 Aug 24 '17 at 6:52
1

Its an interesting question as there are now two ways of altering forms.

Using HOOK_form_alter ie book_form_node_form_alter with $form by ref and by inheritance returning $form as done in ie \Drupal\forum\Form\Overview::buildForm

class Overview extends OverviewTerms {
  ...
  public function buildForm(array $form, FormStateInterface $form_state) {
    ...
    $form = parent::buildForm($form, $form_state, $vocabulary);
    ...
    return $form;
  }
  ...
}

where \Drupal\taxonomy\Form\OverviewTerms::buildForm gets called using $form = parent::buildForm($form, $form_state, $vocabulary); thus in fact zapping $form.

Most code seems to return parent::buildForm($form, $form_state, ...); which we could call 'method' 3.

I guess hook_form_alter will disappear in the next version (10.x?) in favour of event listeners

  • I would not think of inheritance as a way of "altering" forms. For me, form_alter and form building are distinct concepts. – donquixote Aug 24 '17 at 14:45
  • As for the parent::buildForm($form, ..);, here it depends what the parent implementation does. Sometimes the parent implementation zaps, sometimes it does not. – donquixote Aug 24 '17 at 14:47
  • We can conclude form building can be done in several different ways which is confusing :-) ... regarding inheritance check what I did with class TourBuilderListBuilder extends TourListBuilder where I replace the TourListBuilder. It's altering by tour_builder_entity_type_alter – Clemens Tolboom Aug 26 '17 at 14:00
  • Do you mean this? cgit.drupalcode.org/tour_builder/tree/src/… – donquixote Aug 26 '17 at 17:02
  • My previous comment was misleading. Sure you can "alter" the form of a parent form builder. But this does not explain the $form parameter being passed into the form builder. – donquixote Aug 26 '17 at 17:04

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.