4

I'm not able to understand what parent::form($form, $form_state) does in a content entity form. If I comment it out, the entity form still get rendered.

class AdvertiserForm extends ContentEntityForm {
  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);
    /** @var \Drupal\redirect\Entity\Redirect $redirect */
    $advertiser = $this->entity;
    $form['status_code'] = array(
      '#type' => 'textfield',
      '#title' => t('Redirect status'),

    );

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
      $form = parent::form($form, $form_state);

  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $this->entity->save();
    drupal_set_message(t('The redirect has been saved.'));

  }
}

What does parent::form($form, $form_state) do, exactly?

4

It's executing a method with the same name on its parent class. This is standard fare in any language when you're dealing with inheritance, when functionality from the base class is still (or may be in the future) required.

In this case it's the ContentEntityForm::form() method that will be invoked. The fact that it doesn't appear to do/change anything is irrelevant - something might change in the future that does require that base class's method to be called, and by referencing it now, you're future-proofed.

Have a read up on OO PHP if this concept is unfamiliar to you, it's one of the more basic ones, so easy to learn. There are a lot of OO concepts used in D8, learning them early on will really help you get to grips with the framework more quickly.

  • 1
    Just to add to this answer, ContentEntityForm is the name of the object, form() is the name of the method, and :: means that this is a static method. – No Sssweat May 21 '16 at 1:03
  • 2
    It isn't a static method. When referring to a method in the documentation, that is the way of doing it: Class::method(). – kiamlaluno May 21 '16 at 6:45
7

If you look at ContentEntityForm::form(), the method called with parent::form() since ContentEntityForm is the parent class, you will notice what exactly it is doing.

  $form = parent::form($form, $form_state);
  // Content entity forms do not use the parent's #after_build callback
  // because they only need to rebuild the entity in the validation and the
  // submit handler because Field API uses its own #after_build callback for
  // its widgets.
  unset($form['#after_build']);

  $this->getFormDisplay($form_state)->buildForm($this->entity, $form, $form_state);
  // Allow modules to act before and after form language is updated.
  $form['#entity_builders']['update_form_langcode'] = [$this, 'updateFormLangcode'];
  return $form;

It doesn't significantly change the form, so it is expected the form doesn't apparently change, or it is rendered the same if you don't call parent::form(), but (as the comment says), it allows modules to act when the form language is updated. You could not see anything different, but you are actually avoiding modules can do anything when the form language is changed.

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.