2

I am trying to have my module alter menu_ui functions. Changing my module's weight didnt help and I read that hook_module_implements_alter is the way to go. So, as described here I am appending my module to the end of the implementations:

/**
 * Implements hook_module_implements_alter.
 */
function mymodule_module_implements_alter(&$implementations, $hook) {
  if ('form_alter' == $hook) {
    $group = $implementations['mecca_hierarchy_access'];
    unset($implementations['mecca_hierarchy_access']);
    $implementations['mecca_hierarchy_access'] = $group;
  }
}

But this just throws:

Uncaught PHP Exception RuntimeException: "An invalid implementation mymodule_form_alter was added by hook_module_implements_alter()" at /path/to/core/lib/Drupal/Core/Extension/ModuleHandler.php line 591

My form alter looks like this:

function mymodule_form_node_form_alter(&$form, FormStateInterface $form_state) {
  // commented code
}

What do I do wrong?

0

1 Answer 1

4

The exception is raised from the following code. (See ModuleHandler::buildImplementationInfo().)

// If a new implementation was added, verify that the function exists.
if (!function_exists($module . '_' . $hook)) {
  throw new \RuntimeException("An invalid implementation {$module}_{$hook} was added by hook_module_implements_alter()");
}

In your case, it simply happens because there isn't any mecca_hierarchy_access_form_alter() function.

Since you want Drupal to invoke the hook_form_alter() implementation of your module after the other hook_form_alter() implementations, and since mymodule seems to be your module's machine name, this is the code you want to use.

/**
 * Implements hook_module_implements_alter().
 */
function mymodule_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'form_alter' && isset($implementations['mymodule'])) {
    $group = $implementations['mymodule'];
    unset($implementations['mymodule']);
    $implementations['mymodule'] = $group;
  }
}

Always verify the implementation of your module hasn't been already removed from another module, or your hook implementation would actually set $implementations['mymodule'] to NULL.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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