Skip to content

Instantly share code, notes, and snippets.

@andydempster
Last active May 9, 2017 11:01
Show Gist options
  • Save andydempster/e183cb077ee012cb1adb80b9f9ab1a4f to your computer and use it in GitHub Desktop.
Save andydempster/e183cb077ee012cb1adb80b9f9ab1a4f to your computer and use it in GitHub Desktop.
Alter the node label pre-theme - uses callback_entity_info_label

This code came as a result of using the following modules in Drupal 7:

node_display_title

entity_reference_multiple

The output returned from entity ref mult was unalterable and used the default entity label in the markup. I was unable to theme the field and didn't want to alter the markup so implemented these hooks to change the label itself. Rather dangerous but it does fall back gracefully to the default node title.

/**
* Implements hook_entity_info_alter().
*/
function YOUR_MODULE_entity_info_alter(&$entity_info) {
// Use a custom label returner.
$entity_info['node']['label callback'] = '_YOUR_MODULE_set_display_title';
}
/**
* Custom label function that returns display title if set, falls back to
* standard node title.
*
* @see callback_entity_info_label
*/
function _YOUR_MODULE_set_display_title($entity, $entity_type) {
if (isset($entity->field_node_display_title[LANGUAGE_NONE]) && !empty($entity->field_node_display_title[LANGUAGE_NONE][0]['value'])) {
return filter_xss($entity->field_node_display_title[LANGUAGE_NONE][0]['value']);
}
return empty($entity->title) ? 'Untitled entity' : filter_xss($entity->title);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment