I'm following this tutorial on creating a settings form for a module in Drupal 8. The instructions say to implement the buildForm method of ConfigFormBase in the following way:

  public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
    $types = node_type_get_names();
    $config = $this->config('rsvplist.settings');
    $form['rsvplist_types'] = array(
      '#type' => 'checkboxes',
      '#title' => $this->t('The content types to enable RSVP collection for'),
      '#default_value' => $config->get('allowed_types'),
      '#options' => $types,
      '#description' => t('On the specified node types, an RSVP option will be available and can be enabled while that node is being edited.'),

    $form['array_filter'] = array('#type' => 'value', '#value' => TRUE);

    return parent::buildForm($form, $form_state);

Everything here makes sense to me except I can't find any documentation on the role that they 'array_filter' key in the $form variable plays. What does this value do? Where is it documented?

  • Consider switching to the short array syntax. Same as behind the link to the settings form in my answer. It's also coding standard now, see Arrays should be formatted using short array syntax. – leymannx Sep 9 at 13:36
  • @leymannx I wasn't aware that this notation existed, thanks for that. It's much neater. – quant Sep 9 at 22:57

Nice catch! It does nothing. You can safely remove it. Looks like some kind of forgotten relict.

Ah, yeah. The same pattern can be found in core's book module: BookSettingsForm.php.

And there is a 12(!) years old open issue to have it removed. [#315176] Clean-up remains of $form['array_filter'] hack with array_filter in book module

| improve this answer | |

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.