entity_field_value.inc

  1. cis7 sites/all/modules/ulmus/ctools/plugins/access/entity_field_value.inc
  2. cle7 sites/all/modules/ulmus/ctools/plugins/access/entity_field_value.inc
  3. ecd7 sites/all/modules/ulmus/ctools/plugins/access/entity_field_value.inc
  4. elmsmedia7 sites/all/modules/ulmus/ctools/plugins/access/entity_field_value.inc
  5. harmony7 sites/all/modules/ulmus/ctools/plugins/access/entity_field_value.inc
  6. icor7 sites/all/modules/ulmus/ctools/plugins/access/entity_field_value.inc
  7. meedjum_blog7 sites/all/modules/ulmus/ctools/plugins/access/entity_field_value.inc
  8. mooc7 sites/all/modules/ulmus/ctools/plugins/access/entity_field_value.inc

Plugin to provide access control based upon entity bundle.

Functions

File

sites/all/modules/ulmus/ctools/plugins/access/entity_field_value.inc
View source
  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. if (!isset($context->data)) {
  126. // If the context doesn't exist -- for example, a newly added entity
  127. // reference is used as a pane visibility criteria -- we deny access.
  128. return FALSE;
  129. }
  130. list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']);
  131. if ($field_items = field_get_items($entity_type, $context->data, $field_name)) {
  132. $langcode = field_language($entity_type, $context->data, $field_name);
  133. // Get field storage columns.
  134. $instance = field_info_instance($entity_type, $field_name, $bundle_type);
  135. $field = field_info_field_by_id($instance['field_id']);
  136. $columns = array();
  137. foreach ($field['columns'] as $column => $attributes) {
  138. $columns[$column] = _field_sql_storage_columnname($field_name, $column);
  139. }
  140. if (isset($conf[$field_name])) {
  141. // We have settings for this field.
  142. $conf_value_array = _ctools_entity_field_value_ctools_access_get_conf_field_values($conf[$field_name], $langcode);
  143. if (empty($conf_value_array)) {
  144. return FALSE;
  145. }
  146. // Check field value.
  147. foreach ($field_items as $field_value) {
  148. foreach ($field_value as $field_column => $value) {
  149. // Iterate through config values.
  150. foreach ($conf_value_array as $conf_value) {
  151. // Check access only for stored in config column values.
  152. if (isset($conf_value[$field_column]) && $value == $conf_value[$field_column]) {
  153. return TRUE;
  154. }
  155. }
  156. }
  157. }
  158. }
  159. }
  160. return FALSE;
  161. }
  162. function _ctools_entity_field_value_ctools_access_get_conf_field_values($values, $langcode = LANGUAGE_NONE) {
  163. if (!is_array($values) || !isset($values[$langcode])) {
  164. return;
  165. }
  166. $conf_values = array();
  167. foreach ($values[$langcode] as $delta => $value) {
  168. $conf_values[$delta] = $value;
  169. }
  170. return $conf_values;
  171. }
  172. /**
  173. * Provide a summary description based upon the checked entity_bundle.
  174. */
  175. function ctools_entity_field_value_ctools_access_summary($conf, $context, $plugin) {
  176. list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']);
  177. $instances = field_info_instances($entity_type, $bundle_type);
  178. $instance = $instances[$field_name];
  179. $field = field_info_field_by_id($instance['field_id']);
  180. $entity_info = entity_get_info($entity_type);
  181. $entity = (object)array(
  182. $entity_info['entity keys']['bundle'] => $bundle_type,
  183. );
  184. $string = '';
  185. $keys = array();
  186. $values = array();
  187. foreach ($field['columns'] as $column => $attributes) {
  188. $conf_key = _field_sql_storage_columnname($field_name, $column);
  189. if (count($field['columns']) > 1) {
  190. // Add some sort of handling for compound fields
  191. }
  192. else {
  193. if (isset($conf[$conf_key])) {
  194. $entity->{$field_name}[LANGUAGE_NONE][] = array($column => $conf[$conf_key]);
  195. }
  196. }
  197. $string .= " @{$column} equals @{$column}_value";
  198. $keys['@' . $column] = $column;
  199. $values["@{$column}_value"] = $conf[$conf_key];
  200. }
  201. $view_mode = 'full';
  202. $null = NULL;
  203. $options = array('language' => LANGUAGE_NONE);
  204. ctools_include('fields');
  205. $display = field_get_display($instance, $view_mode, $entity);
  206. if (isset($display['module'])) {
  207. $display['type'] = 'list_default';
  208. $function = $display['module'] . '_field_formatter_view';
  209. $items = isset($entity->{$field_name}[LANGUAGE_NONE]) ? $entity->{$field_name}[LANGUAGE_NONE] : array();
  210. if (function_exists($function)) {
  211. $elements = $function($entity_type, $entity, $field, $instance, LANGUAGE_NONE, $items, $display);
  212. }
  213. $value_keys = array_keys($values);
  214. foreach ($value_keys as $key => $value) {
  215. $values[$value] = isset($elements[$key]['#markup']) ? $elements[$key]['#markup'] : '';
  216. }
  217. }
  218. $values = array_merge($keys, $values);
  219. return t($string, $values);
  220. }
Error | ELMSLN API

Error

×

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/elmsln_community/api.elmsln.org/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
  • Error: Call to undefined function apc_delete() in DrupalAPCCache->clear() (line 289 of /var/www/html/elmsln_community/api.elmsln.org/sites/all/modules/apc/drupal_apc_cache.inc).
The website encountered an unexpected error. Please try again later.