entity_property.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide an relationship handler for any entity property.
  5. */
  6. /**
  7. * Plugins are described by creating a $plugin array which will be used
  8. * by the system that includes this file.
  9. */
  10. $plugin = array(
  11. 'title' => t('Entity property or field (via Entity Metadata Wrapper)'),
  12. 'description' => t('Creates any kind of context for entity properties and fields.'),
  13. 'context' => 'entity_entity_property_context',
  14. 'required context' => new ctools_context_required(t('Entity'), 'entity'),
  15. 'edit form' => 'entity_entity_property_edit_form',
  16. 'edit form validate' => 'entity_entity_property_edit_form_validate',
  17. 'defaults' => array(
  18. 'selector' => '',
  19. 'target_context' => 'entity',
  20. 'concatenator' => ','
  21. ),
  22. );
  23. /**
  24. * Return a new context based on an existing context.
  25. */
  26. function entity_entity_property_context($context, $conf) {
  27. $plugin = $conf['name'];
  28. // If unset it wants a generic, unfilled context, which is just NULL.
  29. if (empty($context->data)) {
  30. return ctools_context_create_empty(isset($conf['target_context']) ? $conf['target_context'] : 'entity', NULL);
  31. }
  32. list($part1, $entity_type) = explode(':', $context->plugin);
  33. if (isset($context->data) && $entity = $context->data) {
  34. // Apply the selector.
  35. $wrapper = entity_metadata_wrapper($entity_type, $entity);
  36. $parts = explode(':', $conf['selector']);
  37. try {
  38. foreach ($parts as $part) {
  39. if (!($wrapper instanceof EntityStructureWrapper || $wrapper instanceof EntityListWrapper)) {
  40. $wrapper = NULL;
  41. $value = NULL;
  42. continue;
  43. }
  44. $wrapper = $wrapper->get($part);
  45. }
  46. }
  47. catch (EntityMetadataWrapperException $e) {
  48. $wrapper = NULL;
  49. $value = NULL;
  50. }
  51. if (isset($wrapper)) {
  52. $value = $wrapper->value();
  53. }
  54. // Massage list values.
  55. if ($wrapper instanceof EntityListWrapper) {
  56. $value = $wrapper[0]->value();
  57. $argument = implode($conf['concatenator'], $wrapper->value(array('identifier' => TRUE)));
  58. }
  59. elseif ($wrapper instanceof EntityDrupalWrapper) {
  60. $argument = $wrapper->getIdentifier();
  61. }
  62. // Bail out if we were unable to retrieve the value.
  63. if (!isset($value)) {
  64. return ctools_context_create_empty(isset($conf['target_context']) ? $conf['target_context'] : 'entity', NULL);
  65. }
  66. $context = ctools_context_create($conf['target_context'], $value);
  67. // Provide a suiting argument if context is used as argument.
  68. if (isset($argument)) {
  69. $context->argument = $argument;
  70. }
  71. return $context;
  72. }
  73. }
  74. function entity_entity_property_edit_form($form, &$form_state) {
  75. $conf = $form_state['conf'];
  76. $form['selector'] = array(
  77. '#type' => 'textfield',
  78. '#title' => t('Data selector'),
  79. '#description' => t('Any valid data selector, e.g. "title" to select a node\'s title, or "field_tags:1" to select the second tag.'),
  80. '#default_value' => $conf['selector'],
  81. '#required' => TRUE,
  82. );
  83. $form['concatenator'] = array(
  84. '#title' => t('Concatenator (if multiple)'),
  85. '#type' => 'select',
  86. '#options' => array(',' => ', (AND)', '+' => '+ (OR)'),
  87. '#default_value' => $conf['concatenator'],
  88. '#prefix' => '<div class="clearfix">',
  89. '#suffix' => '</div>',
  90. '#description' => t("When the resulting value is multiple valued and the context is passed on to a view as argument, the value can be concatenated in the form of 1+2+3 (for OR) or 1,2,3 (for AND)."),
  91. );
  92. return $form;
  93. }
  94. function entity_entity_property_edit_form_validate($form, &$form_state) {
  95. $context_key = $form_state['values']['context'];
  96. $context = $form_state['contexts'][$context_key];
  97. $entity_type = $context->type[2];
  98. try {
  99. $all_properties = entity_get_all_property_info($entity_type);
  100. $wrapper = entity_metadata_wrapper($entity_type, NULL, array('property info' => $all_properties));
  101. $parts = explode(':', $form_state['values']['selector']);
  102. foreach ($parts as $part) {
  103. if (!($wrapper instanceof EntityStructureWrapper || $wrapper instanceof EntityListWrapper)) {
  104. form_set_error('selector', t('Unable to apply the data selector part %key.'. array('%key' => $part)));
  105. continue;
  106. }
  107. $wrapper = $wrapper->get($part);
  108. }
  109. $type = entity_entity_property_map_data_type($wrapper->type());
  110. $form_state['conf']['target_context'] = $type;
  111. }
  112. catch (EntityMetadataWrapperException $e) {
  113. form_set_error('selector', t('Unable to apply the data selector on entity type %type. @reason', array('@reason' => $e->getMessage(), '%type' => $entity_type)));
  114. }
  115. // Auto-generate a sane identifier.
  116. if ($form_state['values']['identifier'] == $form['identifier']['#default_value']) {
  117. $form_state['values']['identifier'] = 'Entity property ' . $entity_type . ':' . check_plain($form_state['values']['selector']);
  118. }
  119. }
  120. /**
  121. * Maps an entity-property data type to ctools types.
  122. */
  123. function entity_entity_property_map_data_type($type) {
  124. // Remove list<> wrappers from types.
  125. while ($item_type = entity_property_list_extract_type($type)) {
  126. $type = $item_type;
  127. }
  128. // Massage data type of entites to c
  129. if (entity_get_info($type)) {
  130. $type = "entity:$type";
  131. }
  132. if ($type == 'text') {
  133. $type = 'string';
  134. }
  135. return $type;
  136. }