entity_field_value.inc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide access control based upon entity bundle.
  5. */
  6. $plugin = array(
  7. 'title' => t("(Custom) Entity: Field Value"),
  8. 'description' => t('Control access by entity field value.'),
  9. 'callback' => 'ctools_entity_field_value_ctools_access_check',
  10. 'default' => array('type' => array()),
  11. 'settings form' => 'ctools_entity_field_value_ctools_access_settings',
  12. 'settings form submit' => 'ctools_entity_field_value_ctools_access_settings_submit',
  13. 'summary' => 'ctools_entity_field_value_ctools_access_summary',
  14. 'get child' => 'ctools_entity_field_value_ctools_access_get_child',
  15. 'get children' => 'ctools_entity_field_value_ctools_access_get_children',
  16. );
  17. function ctools_entity_field_value_ctools_access_get_child($plugin, $parent, $child) {
  18. $plugins = &drupal_static(__FUNCTION__, array());
  19. if (empty($plugins[$parent . ':' . $child])) {
  20. list($entity_type, $bundle_type, $field_name) = explode(':', $child);
  21. $plugins[$parent . ':' . $child] = _ctools_entity_field_value_ctools_access_get_child($plugin, $parent, $entity_type, $bundle_type, $field_name);
  22. }
  23. return $plugins[$parent . ':' . $child];
  24. }
  25. function ctools_entity_field_value_ctools_access_get_children($plugin, $parent) {
  26. $plugins = &drupal_static(__FUNCTION__, array());
  27. if (!empty($plugins)) {
  28. return $plugins;
  29. }
  30. $entities = entity_get_info();
  31. foreach ($entities as $entity_type => $entity) {
  32. foreach ($entity['bundles'] as $bundle_type => $bundle) {
  33. foreach (field_info_instances($entity_type, $bundle_type) as $field_name => $field) {
  34. if (!isset($plugins[$parent . ':' . $entity_type . ':' . $bundle_type . ':' . $field_name])) {
  35. $plugin = _ctools_entity_field_value_ctools_access_get_child($plugin, $parent, $entity_type, $bundle_type, $field_name, $entity, $bundle, $field);
  36. $plugins[$parent . ':' . $entity_type . ':' . $bundle_type . ':' . $field_name] = $plugin;
  37. }
  38. }
  39. }
  40. }
  41. return $plugins;
  42. }
  43. function _ctools_entity_field_value_ctools_access_get_child($plugin, $parent, $entity_type, $bundle_type, $field_name, $entity = NULL, $bundle = NULL, $field = NULL) {
  44. // check that the entity, bundle and field arrays have a value.
  45. // If not, load theme using machine names.
  46. if (empty($entity)) {
  47. $entity = entity_get_info($entity_type);
  48. }
  49. if (empty($bundle)) {
  50. $bundle = $entity['bundles'][$bundle_type];
  51. }
  52. if (empty($field)) {
  53. $field_instances = field_info_instances($entity_type, $bundle_type);
  54. $field = $field_instances[$field_name];
  55. }
  56. $plugin['title'] = t('@entity @type: @field Field', array('@entity' => $entity['label'], '@type' => $bundle_type, '@field' => $field['label']));
  57. $plugin['keyword'] = $entity_type;
  58. $plugin['description'] = t('Control access by @entity entity bundle.', array('@entity' => $entity_type));
  59. $plugin['name'] = $parent . ':' . $entity_type . ':' . $bundle_type . ':' . $field_name;
  60. $plugin['required context'] = new ctools_context_required(t(ucfirst($entity_type)), $entity_type, array(
  61. 'type' => $bundle_type,
  62. ));
  63. return $plugin;
  64. }
  65. /**
  66. * Settings form for the 'by entity_bundle' access plugin
  67. */
  68. function ctools_entity_field_value_ctools_access_settings($form, &$form_state, $conf) {
  69. $plugin = $form_state['plugin'];
  70. list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']);
  71. $entity_info = entity_get_info($entity_type);
  72. $instances = field_info_instances($entity_type, $bundle_type);
  73. $instance = $instances[$field_name];
  74. $field = field_info_field_by_id($instance['field_id']);
  75. foreach ($field['columns'] as $column => $attributes) {
  76. $columns[] = _field_sql_storage_columnname($field_name, $column);
  77. }
  78. ctools_include('fields');
  79. $entity = (object)array(
  80. $entity_info['entity keys']['bundle'] => $bundle_type,
  81. );
  82. $langcode = field_valid_language(NULL);
  83. $form['settings'] += (array) ctools_field_invoke_field($instance, 'form', $entity_type, $entity, $form, $form_state, array('default' => TRUE, 'language' => $langcode));
  84. // weight is really not important once this is populated and will only interfere with the form layout.
  85. foreach (element_children($form['settings']) as $element) {
  86. unset($form['settings'][$element]['#weight']);
  87. }
  88. // Need more logic here to handle compound fields.
  89. foreach ($columns as $column) {
  90. if (isset($conf[$column]) && is_array($conf[$column])) {
  91. foreach ($conf[$column] as $delta => $conf_value) {
  92. if (is_numeric($delta) && is_array($conf_value)) {
  93. $form['settings'][$field_name][LANGUAGE_NONE][$delta]['value']['#default_value'] = $conf_value['value'];
  94. }
  95. }
  96. }
  97. else {
  98. $form['settings'][$field_name][LANGUAGE_NONE]['#default_value'] = $conf[$column];
  99. }
  100. }
  101. return $form;
  102. }
  103. /**
  104. * Compress the entity bundles allowed to the minimum.
  105. */
  106. function ctools_entity_field_value_ctools_access_settings_submit($form, &$form_state) {
  107. $plugin = $form_state['plugin'];
  108. list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']);
  109. $langcode = field_valid_language(NULL);
  110. $langcode = isset($form_state['input']['settings'][$field_name][$langcode]) ? $langcode : LANGUAGE_NONE;
  111. $instances = field_info_instances($entity_type, $bundle_type);
  112. $instance = $instances[$field_name];
  113. $field = field_info_field_by_id($instance['field_id']);
  114. foreach ($field['columns'] as $column => $attributes) {
  115. $columns[] = _field_sql_storage_columnname($field_name, $column);
  116. }
  117. foreach ($columns as $column) {
  118. $form_state['values']['settings'][$column] = $form_state['input']['settings'][$field_name][$langcode];
  119. }
  120. }
  121. /**
  122. * Check for access.
  123. */
  124. function ctools_entity_field_value_ctools_access_check($conf, $context, $plugin) {
  125. list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']);
  126. if ($field_items = field_get_items($entity_type, $context->data, $field_name)) {
  127. $langcode = field_language($entity_type, $context->data, $field_name);
  128. // Get field storage columns.
  129. $instance = field_info_instance($entity_type, $field_name, $bundle_type);
  130. $field = field_info_field_by_id($instance['field_id']);
  131. $columns = array();
  132. foreach ($field['columns'] as $column => $attributes) {
  133. $columns[$column] = _field_sql_storage_columnname($field_name, $column);
  134. }
  135. foreach ($conf as $potential_field => $values) {
  136. if ($field_name === $potential_field) {
  137. $conf_value_array = _ctools_entity_field_value_ctools_access_get_conf_field_values($values, $langcode);
  138. if (empty($conf_value_array)) {
  139. return FALSE;
  140. }
  141. // Check field value.
  142. foreach ($field_items as $field_value) {
  143. foreach ($field_value as $field_column => $value) {
  144. // Iterate through config values.
  145. foreach ($conf_value_array as $conf_value) {
  146. //
  147. if ($value == $conf_value[$field_column]) {
  148. return TRUE;
  149. }
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }
  156. return FALSE;
  157. }
  158. function _ctools_entity_field_value_ctools_access_get_conf_field_values($values, $langcode = LANGUAGE_NONE) {
  159. if (!is_array($values) || !isset($values[$langcode])) {
  160. return;
  161. }
  162. $conf_values = array();
  163. foreach ($values[$langcode] as $delta => $value) {
  164. $conf_values[$delta] = $value;
  165. }
  166. return $conf_values;
  167. }
  168. /**
  169. * Provide a summary description based upon the checked entity_bundle.
  170. */
  171. function ctools_entity_field_value_ctools_access_summary($conf, $context, $plugin) {
  172. list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']);
  173. $instances = field_info_instances($entity_type, $bundle_type);
  174. $instance = $instances[$field_name];
  175. $field = field_info_field_by_id($instance['field_id']);
  176. $entity_info = entity_get_info($entity_type);
  177. $entity = (object)array(
  178. $entity_info['entity keys']['bundle'] => $bundle_type,
  179. );
  180. $string = '';
  181. $keys = array();
  182. $values = array();
  183. foreach ($field['columns'] as $column => $attributes) {
  184. $conf_key = _field_sql_storage_columnname($field_name, $column);
  185. if (count($field['columns']) > 1) {
  186. // Add some sort of handling for compound fields
  187. }
  188. else {
  189. if (isset($conf[$conf_key])) {
  190. $entity->{$field_name}[LANGUAGE_NONE][] = array($column => $conf[$conf_key]);
  191. }
  192. }
  193. $string .= " @{$column} equals @{$column}_value";
  194. $keys['@' . $column] = $column;
  195. $values["@{$column}_value"] = $conf[$conf_key];
  196. }
  197. $view_mode = 'full';
  198. $null = NULL;
  199. $options = array('language' => LANGUAGE_NONE);
  200. ctools_include('fields');
  201. $display = field_get_display($instance, $view_mode, $entity);
  202. $display['type'] = 'list_default';
  203. $function = $display['module'] . '_field_formatter_view';
  204. $items = isset($entity->{$field_name}[LANGUAGE_NONE]) ? $entity->{$field_name}[LANGUAGE_NONE] : array();
  205. if (function_exists($function)) {
  206. $elements = $function($entity_type, $entity, $field, $instance, LANGUAGE_NONE, $items, $display);
  207. }
  208. $value_keys = array_keys($values);
  209. foreach ($value_keys as $key => $value) {
  210. $values[$value] = $elements[$key]['#markup'];
  211. }
  212. $values = array_merge($keys, $values);
  213. return t($string, $values);
  214. }