entity_view.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * @file
  4. * Content type plugin to expose rendered entities, view mode configuration
  5. * still available.
  6. */
  7. $plugin = array(
  8. 'title' => t('Rendered entity'),
  9. 'defaults' => array('view_mode' => 'full'),
  10. 'content type' => 'entity_entity_view_content_type_info',
  11. );
  12. /**
  13. * Get the entity content type info.
  14. */
  15. function entity_entity_view_content_type_info($entity_type) {
  16. $types = entity_entity_view_content_type_content_types();
  17. if (isset($types[$entity_type])) {
  18. return $types[$entity_type];
  19. }
  20. }
  21. /**
  22. * Implements hook_PLUGIN_content_type_content_types().
  23. *
  24. * Rendered entity use entity types machine name as subtype name.
  25. */
  26. function entity_entity_view_content_type_content_types() {
  27. $types = array();
  28. $entities = entity_get_info();
  29. foreach ($entities as $entity_type => $info) {
  30. if (entity_type_supports($entity_type, 'view')) {
  31. $types[$entity_type] = array(
  32. 'title' => t('Rendered @entity_type', array('@entity_type' => $info['label'])),
  33. 'category' => t('Entity'),
  34. 'required context' => new ctools_context_required(t('Entity'), $entity_type),
  35. );
  36. }
  37. }
  38. return $types;
  39. }
  40. /**
  41. * Returns an edit form for a entity.
  42. *
  43. * Rendered entity use entity types machine name as subtype name.
  44. *
  45. * @see entity_entity_view_get_content_types()
  46. */
  47. function entity_entity_view_content_type_edit_form($form, &$form_state) {
  48. $conf = $form_state['conf'];
  49. $entity_type = $form_state['subtype_name'];
  50. $entity_info = entity_get_info($entity_type);
  51. $options = array();
  52. if (!empty($entity_info['view modes'])) {
  53. foreach ($entity_info['view modes'] as $mode => $settings) {
  54. $options[$mode] = $settings['label'];
  55. }
  56. }
  57. if (count($options) > 1) {
  58. $form['view_mode'] = array(
  59. '#type' => 'select',
  60. '#options' => $options,
  61. '#title' => t('View mode'),
  62. '#default_value' => $conf['view_mode'],
  63. );
  64. }
  65. else {
  66. $form['view_mode_info'] = array(
  67. '#type' => 'item',
  68. '#title' => t('View mode'),
  69. '#description' => t('Only one view mode is available for this entity type.'),
  70. '#markup' => $options ? current($options) : t('Default'),
  71. );
  72. $form['view_mode'] = array(
  73. '#type' => 'value',
  74. '#value' => $options ? key($options) : 'default',
  75. );
  76. }
  77. return $form;
  78. }
  79. /**
  80. * Save selected view mode.
  81. */
  82. function entity_entity_view_content_type_edit_form_submit(&$form, &$form_state) {
  83. if (isset($form_state['values']['view_mode'])) {
  84. $form_state['conf']['view_mode'] = $form_state['values']['view_mode'];
  85. }
  86. }
  87. /**
  88. * Implements hook_PLUGIN_content_type_render().
  89. *
  90. * Ctools requires us to return a block.
  91. *
  92. * @see ctools_content_render()
  93. */
  94. function entity_entity_view_content_type_render($entity_type, $conf, $panel_args, $context) {
  95. if ($context->empty) {
  96. return;
  97. }
  98. $block = new stdClass();
  99. $block->module = 'entity';
  100. $block->delta = $entity_type . '-' . str_replace('-', '_', $conf['view_mode']);
  101. $entity_id = $context->argument;
  102. $entity = entity_load_single($entity_type, $entity_id);
  103. $block->content = entity_view($entity_type, array($entity_id => $entity), $conf['view_mode']);
  104. return $block;
  105. }
  106. /**
  107. * Implements hook_PLUGIN_content_type_admin_title().
  108. *
  109. * Returns the administrative title for a type.
  110. */
  111. function entity_entity_view_content_type_admin_title($entity_type, $conf, $contexts) {
  112. $entity_info = entity_get_info($entity_type);
  113. $view_mode = $conf['view_mode'];
  114. if (isset($entity_info['view modes'][$view_mode])) {
  115. $view_mode = $entity_info['view modes'][$view_mode]['label'];
  116. }
  117. return t('Rendered @entity_type using view mode "@view_mode"', array('@entity_type' => $entity_info['label'], '@view_mode' => $view_mode));
  118. }