Skip to content

Instantly share code, notes, and snippets.

@opi
Last active October 12, 2017 09:32
Show Gist options
  • Select an option

  • Save opi/4024942 to your computer and use it in GitHub Desktop.

Select an option

Save opi/4024942 to your computer and use it in GitHub Desktop.
Drupal: Add node extra field
<?php
/**
* Implements hook_field_extra_fields().
*/
function mymodule_field_extra_fields() {
// $extra['entity_type']['bundle']
$extra['node']['my_content_type'] = array(
'display' => array(
'my_extra_field' => array(
'label' => t("My Extra field"),
'description' => t("This is my extra field."),
'weight' => 10, // Default weight
),
)
);
return $extra;
}
/**
* Implements hook_node_view().
*/
function mymodule_node_view($node, $view_mode, $langcode) {
// Get extra field status
$extra_fields = field_extra_fields_get_display('node', $node->type, $view_mode);
if ($node->type == 'my_content_type') {
if (!empty($extra_fields['my_extra_field']['visible'])) {
// Add extra field render array to node content
$node->content['my_extra_field'] = array(
'#markup' => "My extra field content."
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment