Add a pseudo field to a node (or entity) and make it visible for Views and Search API.

Public

First, add a function which returns the content of your field given a specific node.

</> CopyGet raw version
php
  1. /**
  2.  * Getter to return the phone number.
  3.  */
  4. function MY_MODULE_phone($node) {
  5. return '089 / 12345' . $node->nid;
  6. }

Add the property to the node object on load.

</> CopyGet raw version
php
  1. /**
  2.  * Implements hook_node_load().
  3.  */
  4. function MY_MODULE_node_load($nodes, $types) {
  5. if (!in_array('article', $types)) {
  6. return;
  7. }
  8.  
  9. foreach ($nodes as $node) {
  10. // add the project number to each node
  11. $node->phone = MY_MODULE_phone($node);
  12. }
  13. }

Make it available in the display settings of the node type.

</> CopyGet raw version
php
  1. /**
  2.  * Implements hook_field_extra_fields().
  3.  */
  4. function MY_MODULE_field_extra_fields() {
  5. $extra = array();
  6. $extra['node']['article']['display']['phone'] = array(
  7. 'label' => t('Phone number'),
  8. 'description' => t('Phone number'),
  9. 'weight' => 10,
  10. );
  11. return $extra;
  12. }

Add it to the content array when the node is viewed.

</> CopyGet raw version
php
  1. /**
  2.  * Implements hook_node_view
  3.  */
  4. function MY_MODULE_node_view($node, $view_mode, $langcode) {
  5. if ($node->type != 'article') {
  6. return;
  7. }
  8.  
  9. $node->content['phone'] = array(
  10. '#type' => 'item',
  11. '#title' => t('Phone number: '),
  12. '#markup' => $node->phone,
  13. );
  14. }

Make it available for Entity API (Rules / Search API).

</> CopyGet raw version
php
  1. /**
  2.  * Implements hook_entity_property_info_alter().
  3.  *
  4.  * Note: This hook is provided by Entity API.
  5.  */
  6. function MY_MODULE_entity_property_info_alter(&$info) {
  7. $info['node']['bundles']['article']['properties']['phone'] = array(
  8. 'type' => 'text',
  9. 'label' => t('Phone number'),
  10. 'sanitized' => TRUE,
  11. 'getter callback' => 'MY_MODULE_phone',
  12. );
  13. }

Make it available for Views.

</> CopyGet raw version
php
  1. /**
  2.  * Implements hook_views_data_alter().
  3.  *
  4.  * Note: This hook is provided by Views.
  5.  */
  6. function MY_MODULE_views_data_alter(&$data){
  7. // Entity API seems to add a handler, but at a different place.
  8. $data['node']['phone'] = $data['entity_node']['phone'];
  9. }

Comments

mattew's picture

Thanks for the trick.
You may have to test if the extra field is visible in the view mode before adding it yo the $node->content or it will be displayed on each node displays (full, teaser, etc.):

  1. /**
  2.  * Implements hook_node_view().
  3.  */
  4. function my_module_node_view($node, $view_mode, $langcode) {
  5. $extrafields = field_extra_fields_get_display('node', $node->type, $view_mode);
  6. $extrafield_name = 'phone';
  7. if (isset($extrafields[$extrafield_name])
  8. && isset($extrafields[$extrafield_name]['visible'])
  9. && $extrafields[$extrafield_name]['visible']) {
  10. $node->content['phone'] = array(
  11. '#type' => 'item',
  12. '#title' => t('Phone number: '),
  13. '#markup' => $node->phone,
  14. );
  15. }
  16. }

Also you will not have to test the $node->type because your extra-field is added to articles only in your hook_field_extra_fields.