entity_field_extra.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. $plugin = array(
  3. 'title' => t('Entity extra field'),
  4. 'defaults' => array('view_mode' => NULL),
  5. 'content type' => 'ctools_entity_field_extra_content_type_content_type',
  6. );
  7. /**
  8. * Just one subtype.
  9. *
  10. * Ordinarily this function is meant to get just one subtype. However, we are
  11. * using it to deal with the fact that we have changed the subtype names. This
  12. * lets us translate the name properly.
  13. */
  14. function ctools_entity_field_extra_content_type_content_type($subtype) {
  15. $types = ctools_entity_field_extra_content_type_content_types();
  16. if (isset($types[$subtype])) {
  17. return $types[$subtype];
  18. }
  19. }
  20. /**
  21. * Return all extra field content types available.
  22. */
  23. function ctools_entity_field_extra_content_type_content_types() {
  24. // This will hold all the individual field content types.
  25. $types = &drupal_static(__FUNCTION__);
  26. if (isset($types)) {
  27. return $types;
  28. }
  29. $types = array();
  30. $context_types = array();
  31. $entities = entity_get_info();
  32. foreach ($entities as $entity_type => $entity) {
  33. foreach ($entity['bundles'] as $type => $bundle) {
  34. foreach (field_info_extra_fields($entity_type, $type, 'display') as $field_name => $info) {
  35. if (!isset($types[$entity_type . ':' . $field_name])) {
  36. $types[$entity_type . ':' . $field_name] = array(
  37. 'category' => t(ucfirst($entity_type)),
  38. 'icon' => 'icon_field.png',
  39. 'title' => $info['label'],
  40. 'description' => isset($info['description']) ? $info['description'] : '',
  41. );
  42. }
  43. $context_types[$entity_type . ':' . $field_name]['types'][$type] = $bundle['label'];
  44. }
  45. }
  46. }
  47. // Create the required context for each field related to the bundle types.
  48. foreach ($types as $key => $field_content_type) {
  49. list($entity_type, $field_name) = explode(':', $key, 2);
  50. $types[$key]['required context'] = new ctools_context_required(t(ucfirst($entity_type)), $entity_type, array(
  51. 'type' => array_keys($context_types[$key]['types']),
  52. ));
  53. unset($context_types[$key]['types']);
  54. }
  55. return $types;
  56. }
  57. /**
  58. * Returns an edit form for an extra field.
  59. */
  60. function ctools_entity_field_extra_content_type_edit_form($form, &$form_state) {
  61. $conf = $form_state['conf'];
  62. $subtype = $form_state['subtype_name'];
  63. list($entity_type, $field_name) = explode(':', $subtype, 2);
  64. $info = entity_get_info($entity_type);
  65. $view_mode_options = array();
  66. foreach ($info['view modes'] as $mode => $option) {
  67. $view_mode_options[$mode] = $option['label'];
  68. }
  69. $form['view_mode'] = array(
  70. '#title' => t('View mode'),
  71. '#type' => 'select',
  72. '#description' => t('Select a view mode for this extra field.'),
  73. '#options' => $view_mode_options,
  74. '#default_value' => $conf['view_mode'],
  75. );
  76. return $form;
  77. }
  78. function ctools_entity_field_extra_content_type_edit_form_submit($form, &$form_state) {
  79. $form_state['conf']['view_mode'] = $form_state['values']['view_mode'];
  80. }
  81. /**
  82. * Render the extra field.
  83. */
  84. function ctools_entity_field_extra_content_type_render($subtype, $conf, $panel_args, $context) {
  85. if (empty($context) || empty($context->data)) {
  86. return;
  87. }
  88. // Get a shortcut to the entity.
  89. $entity = clone $context->data;
  90. list($entity_type, $field_name) = explode(':', $subtype, 2);
  91. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  92. $langcode = $GLOBALS['language_content']->language;
  93. $function = $entity_type . '_view';
  94. if (in_array($entity_type, array('node', 'taxonomy_term', 'user')) && function_exists($function)) {
  95. // Call known ENTITY_view() to get the extra field.
  96. $entity->content = $function($entity, $conf['view_mode'], $langcode);
  97. }
  98. else {
  99. // Invoke the view-hook to get the extra field.
  100. $entity->content = array();
  101. module_invoke_all($entity_type . '_view', $entity, $conf['view_mode'], $langcode);
  102. module_invoke_all('entity_view', $entity, $entity_type, $conf['view_mode'], $langcode);
  103. }
  104. if (isset($entity->content[$field_name])) {
  105. // Build the content type block.
  106. $block = new stdClass();
  107. $block->module = 'entity_field_extra';
  108. $block->content = $entity->content[$field_name];
  109. $block->delta = $id;
  110. return $block;
  111. }
  112. }
  113. function ctools_entity_field_extra_content_type_admin_title($subtype, $conf, $context) {
  114. $info = ctools_entity_field_extra_content_type_content_type($subtype);
  115. return t('"@s" @field', array('@s' => $context->identifier, '@field' => $info['title']));
  116. }