5

I want to change the 'access callback' of user entity to my own access function or at least alter the original function 'entity_metadata_user_access'.

The problem is that 'entity module' which I need because of others' module dependencies already alters the 'user entity information' and changes the 'access callback' to its own function 'entity_metadata_user_access' and so when I alter it with my module, the entity module re-alters it to 'entity_metadata_user_access'.

To make it more clear, the entity module is alters the entity info after me which causes that the final value of the callback is not mine (does not come from my alteration) but its and therefore the callback is wrong.

I know that I could modify the module itself but I honestly don't want to do that because it makes later updates painful.

1
  • By overriding the exact Entity Module hook in your module, you should be able to achieve what you are trying to do. Your hook will be called last. Can you paste your code? – Teknotica Nov 11 '13 at 9:37
3

Take a look at hook_module_implements_alter, you can change the order of the hooks called by Drupal and make sure your alter hook is called as last one.

function YOURMODULE_module_implements_alter(&$implementations, $hook) {
    if($hook === 'entity_info_alter') { 
    $group = $implementations['YOURMODULE'];
        unset($implementations['YOURMODULE']);
        $implementations['YOURMODULE'] = $group;
    }
}
4

Here is a complete working example of overriding entity_metadata_user_access:

/**
 * Implements hook_module_implements_alter().
 */
function YOURMODULE_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'entity_info_alter') {
    $group = $implementations['YOURMODULE'];
    unset($implementations['YOURMODULE']);
    $implementations['YOURMODULE'] = $group;
  }
}

/**
 * Implements hook_entity_info_alter().
 */
function YOURMODULE_entity_info_alter(&$entity_info) {
  $entity_info['user']['access callback'] = 'YOURMODULE_entity_metadata_user_access';
}

/**
 * Overrides entity_metadata_user_access function.
 */
function YOURMODULE_entity_metadata_user_access($op, $entity = NULL, $account = NULL, $entity_type = NULL) {
  // Include Entity API module's logic
  $return = entity_metadata_user_access($op, $entity, $account, $entity_type);

  // Do your own logic here:
  // $return = $return || ...

  return $return;
}
1
  • Above code is awesome, Save my Day :) – j2r Sep 6 '16 at 15:41

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.