How can you contact us

2By phone at +40 749 014 360

If you still have problems, please let us know, by sending an email to info@westweb-solutions.com Thank you!

OFFICE HOURS

Mon-Fri 9:00AM - 6:00AM
Sat - 9:00AM - 1:00PM
Sundays by appointment only!

Drupal 8 - Allow other modules to alter your data

/ / Drupal 8

In Drupal 7 we used the function drupal_alter() to allow other modules to alter our data.

This function is not available any more in Drupal 8.
For Drupal 8 we have to use the following code, supposing that $data is our data that can be altered by other modules.


// Create the Data that will be passed to ModuleHandler::alter().
$data = array(
  '#type' => 'markup',
  '#markup' => t('This is the default text from Other Module'),
);

// Allow other modules to alter the $data invoking: hook_TYPE_alter().
// In our case, the TYPE will be 'othermodule_example1'.
\Drupal::moduleHandler()->alter('othermodule_example1', $data);

This code will expose another hook_TYPE_alter() that can be used by other modules to alter the $data variable.

If a module 'othermodule' is using the above code, then you can use the below code to alter the $data variable in your 'mymodule' module.


/**
 * Implements hook_other_module_example1_alter().
 */
function mymodule_othermodule_example1_alter(&$data) {
  // Here we have full access to $data array.
  $data['#markup'] = t('This was altered by MyModule');
}


Sign in your account to have access to different features

Forgot your details?

TOP