Last active
October 12, 2017 09:32
-
-
Save opi/4024942 to your computer and use it in GitHub Desktop.
Drupal: Add node extra field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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