1

I am porting this contrib module to D8 and need some help.

The module is shipped with a custom node type. Certain elements options in the node_form will be populated dynamically. Also other modules needs to able to provide options for these fields.

hook_form_FORM_ID_alter calls the wrapper class MyModuleManager that performs several helper duties. All is going well except that I can not get the custom element_validation to work.

Upon form validation a warning is generated

Warning: call_user_func_array() expects parameter 1 to be a valid callback, class 'FilebrowserManager' not found in Drupal\Core\Form\FormValidator->doValidateForm() (line 282 of core/lib/Drupal/Core/Form/FormValidator.php).

together with the fatal error:

Recoverable fatal error: Argument 1 passed to Drupal\Core\Field\WidgetBase::massageFormValues() must be of the type array, string given, called in /var/www/drupal8/core/lib/Drupal/Core/Field/WidgetBase.php on line 376 and defined in Drupal\Core\Field\WidgetBase->massageFormValues() (line 511 of core/lib/Drupal/Core/Field/WidgetBase.php).

The fatal error occurs exactly when validating $form['my_field']

class MyModuleManager implements MyModuleManagerInterface {
  use StringTranslationTrait;

  public function addFormElements(array $form, FormStateInterface $form_state, NodeInterface $node, AccountInterface $account, $collapsed = TRUE) {
    ...

    return $form;
  }

  public function addFormDefaultOptions(array $form, FormStateInterface $form_state) {

    // Replace MyModule_hook by Events
    // ...
    // Events returns dynamic $options

    $form['my_field']['widget']['#options'] = $options;
    $form['my_field']['widget']['#element_validate'] = array(array($this, 'myModuleValidateTrue()'));

    return $form;
  }

  public function myModuleValidateTrue() {
    return;
  }

}

As the warning tells it can not find the callback in FormValidator I suspect the problem might be the way I wrapped my code in this class. Being new to OOP I would appreciate all help.

| improve this question | | | | |
0

It's probably this line:

$form['my_field']['widget']['#element_validate'] = array(array($this, 'myModuleValidateTrue()'));

Which should be this, without parentheses in the method name:

$form['my_field']['widget']['#element_validate'] = array(array($this, 'myModuleValidateTrue'));

See PHP: Callbacks / Callables for the full story.

| 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.