entityreference.module

  1. cis7 sites/all/modules/ulmus/entityreference/entityreference.module
  2. cle7 sites/all/modules/ulmus/entityreference/entityreference.module
  3. ecd7 sites/all/modules/ulmus/entityreference/entityreference.module
  4. elmsmedia7 sites/all/modules/ulmus/entityreference/entityreference.module
  5. harmony7 sites/all/modules/ulmus/entityreference/entityreference.module
  6. icor7 sites/all/modules/ulmus/entityreference/entityreference.module
  7. icor7 sites/all/modules/local_contrib/entityreference_dynamicselect_widget/entityreference/entityreference.module
  8. meedjum_blog7 sites/all/modules/ulmus/entityreference/entityreference.module
  9. mooc7 sites/all/modules/ulmus/entityreference/entityreference.module

Functions

Namesort descending Description
entityreference_autocomplete_access_callback Menu Access callback for the autocomplete widget.
entityreference_autocomplete_callback Menu callback: autocomplete the label of an entity.
entityreference_autocomplete_callback_get_matches Return JSON based on given field, instance and string.
entityreference_behavior_plugin_process CTools callback; Process the behavoir plugins.
entityreference_ctools_plugin_directory Implements hook_ctools_plugin_directory().
entityreference_ctools_plugin_type Implements hook_ctools_plugin_type().
entityreference_entity_crud Invoke a behavior based on entity CRUD.
entityreference_entity_delete Implements hook_entity_delete().
entityreference_entity_insert Implements hook_entity_insert().
entityreference_entity_update Implements hook_entity_update().
entityreference_field_attach_delete Implements hook_field_attach_delete().
entityreference_field_attach_insert Implements hook_field_attach_insert().
entityreference_field_attach_update Implements hook_field_attach_update().
entityreference_field_delete Implements hook_field_delete().
entityreference_field_formatter_info Implements hook_field_formatter_info().
entityreference_field_formatter_prepare_view Implements hook_field_formatter_prepare_view().
entityreference_field_formatter_settings_form Implements hook_field_formatter_settings_form().
entityreference_field_formatter_settings_summary Implements hook_field_formatter_settings_summary().
entityreference_field_formatter_view Implements hook_field_formatter_view().
entityreference_field_info Implements hook_field_info().
entityreference_field_insert Implements hook_field_insert().
entityreference_field_instance_settings_form Implements hook_field_instance_settings_form().
entityreference_field_is_empty Implements hook_field_is_empty().
entityreference_field_load Implements hook_field_load().
entityreference_field_presave Implements hook_field_presave().
entityreference_field_property_callback Property callback for the Entity Metadata framework.
entityreference_field_settings_form Implements hook_field_settings_form().
entityreference_field_update Implements hook_field_update().
entityreference_field_validate Implements hook_field_validate().
entityreference_field_widget_error Implements hook_field_widget_error().
entityreference_field_widget_form Implements hook_field_widget_form().
entityreference_field_widget_info Implements hook_field_widget_info().
entityreference_field_widget_info_alter Implements hook_field_widget_info_alter().
entityreference_field_widget_settings_form Implements hook_field_widget_settings_form().
entityreference_flush_caches Implements hook_flush_caches().
entityreference_get_accessible_behavior_plugins Get all accessible behavior plugins.
entityreference_get_behavior_handlers Get the behavior handlers for a given entityreference field.
entityreference_get_selection_handler Get the selection handler for a given entityreference field.
entityreference_init Implements hook_init().
entityreference_menu Implements hook_menu().
entityreference_options_list Implements hook_options_list().
entityreference_query_entityreference_alter Implements hook_query_TAG_alter().
entityreference_settings_ajax Ajax callback for the handler settings form.
entityreference_settings_ajax_submit Submit handler for the non-JS case.
entityreference_views_api Implements hook_views_api().
_entityreference_autocomplete_tags_validate
_entityreference_autocomplete_validate
_entityreference_element_validate_filter
_entityreference_field_instance_settings_form
_entityreference_field_instance_settings_validate
_entityreference_field_settings_ajax_process
_entityreference_field_settings_ajax_process_element
_entityreference_field_settings_process
_entityreference_field_settings_validate
_entityreference_form_process_merge_parent
_entityreference_get_behavior_elements Get the field or instance elements for the field configuration.
_entityreference_get_behavior_handler Get the behavior handler for a given entityreference field and instance.

Classes

Namesort descending Description
EntityReferenceRecursiveRenderingException Exception thrown when the entity view renderer goes into a potentially infinite loop.

File

sites/all/modules/ulmus/entityreference/entityreference.module
View source
  1. <?php
  2. /**
  3. * Implements hook_ctools_plugin_directory().
  4. */
  5. function entityreference_ctools_plugin_directory($module, $plugin) {
  6. if ($module == 'entityreference') {
  7. return 'plugins/' . $plugin;
  8. }
  9. }
  10. /**
  11. * Implements hook_init().
  12. */
  13. function entityreference_init() {
  14. // Include feeds.module integration.
  15. if (module_exists('feeds')) {
  16. module_load_include('inc', 'entityreference', 'entityreference.feeds');
  17. }
  18. }
  19. /**
  20. * Implements hook_ctools_plugin_type().
  21. */
  22. function entityreference_ctools_plugin_type() {
  23. $plugins['selection'] = array(
  24. 'classes' => array('class'),
  25. );
  26. $plugins['behavior'] = array(
  27. 'classes' => array('class'),
  28. 'process' => 'entityreference_behavior_plugin_process',
  29. );
  30. return $plugins;
  31. }
  32. /**
  33. * CTools callback; Process the behavoir plugins.
  34. */
  35. function entityreference_behavior_plugin_process(&$plugin, $info) {
  36. $plugin += array(
  37. 'description' => '',
  38. 'behavior type' => 'field',
  39. 'access callback' => FALSE,
  40. 'force enabled' => FALSE,
  41. );
  42. }
  43. /**
  44. * Implements hook_field_info().
  45. */
  46. function entityreference_field_info() {
  47. $field_info['entityreference'] = array(
  48. 'label' => t('Entity Reference'),
  49. 'description' => t('This field reference another entity.'),
  50. 'settings' => array(
  51. // Default to the core target entity type node.
  52. 'target_type' => 'node',
  53. // The handler for this field.
  54. 'handler' => 'base',
  55. // The handler settings.
  56. 'handler_settings' => array(),
  57. ),
  58. 'instance_settings' => array(),
  59. 'default_widget' => 'entityreference_autocomplete',
  60. 'default_formatter' => 'entityreference_label',
  61. 'property_callbacks' => array('entityreference_field_property_callback'),
  62. );
  63. return $field_info;
  64. }
  65. /**
  66. * Implements hook_flush_caches().
  67. */
  68. function entityreference_flush_caches() {
  69. // Because of the intricacies of the info hooks, we are forced to keep a
  70. // separate list of the base tables of each entities, so that we can use
  71. // it in entityreference_field_schema() without calling entity_get_info().
  72. // See http://drupal.org/node/1416558 for details.
  73. $base_tables = array();
  74. foreach (entity_get_info() as $entity_type => $entity_info) {
  75. if (!empty($entity_info['base table']) && !empty($entity_info['entity keys']['id'])) {
  76. $base_tables[$entity_type] = array($entity_info['base table'], $entity_info['entity keys']['id']);
  77. }
  78. }
  79. // We are using a variable because cache is going to be cleared right after
  80. // hook_flush_caches() is finished.
  81. variable_set('entityreference:base-tables', $base_tables);
  82. }
  83. /**
  84. * Implements hook_menu().
  85. */
  86. function entityreference_menu() {
  87. $items = array();
  88. $items['entityreference/autocomplete/single/%/%/%'] = array(
  89. 'title' => 'Entity Reference Autocomplete',
  90. 'page callback' => 'entityreference_autocomplete_callback',
  91. 'page arguments' => array(2, 3, 4, 5),
  92. 'access callback' => 'entityreference_autocomplete_access_callback',
  93. 'access arguments' => array(2, 3, 4, 5),
  94. 'type' => MENU_CALLBACK,
  95. );
  96. $items['entityreference/autocomplete/tags/%/%/%'] = array(
  97. 'title' => 'Entity Reference Autocomplete',
  98. 'page callback' => 'entityreference_autocomplete_callback',
  99. 'page arguments' => array(2, 3, 4, 5),
  100. 'access callback' => 'entityreference_autocomplete_access_callback',
  101. 'access arguments' => array(2, 3, 4, 5),
  102. 'type' => MENU_CALLBACK,
  103. );
  104. return $items;
  105. }
  106. /**
  107. * Implements hook_field_is_empty().
  108. */
  109. function entityreference_field_is_empty($item, $field) {
  110. $empty = !isset($item['target_id']) || !is_numeric($item['target_id']);
  111. // Invoke the behaviors to allow them to override the empty status.
  112. foreach (entityreference_get_behavior_handlers($field) as $handler) {
  113. $handler->is_empty_alter($empty, $item, $field);
  114. }
  115. return $empty;
  116. }
  117. /**
  118. * Get the behavior handlers for a given entityreference field.
  119. */
  120. function entityreference_get_behavior_handlers($field, $instance = NULL) {
  121. $object_cache = drupal_static(__FUNCTION__);
  122. $identifier = $field['field_name'];
  123. if (!empty($instance)) {
  124. $identifier .= ':' . $instance['entity_type'] . ':' . $instance['bundle'];
  125. }
  126. if (!isset($object_cache[$identifier])) {
  127. $object_cache[$identifier] = array();
  128. // Merge in defaults.
  129. $field['settings'] += array('behaviors' => array());
  130. $object_cache[$field['field_name']] = array();
  131. $behaviors = !empty($field['settings']['handler_settings']['behaviors']) ? $field['settings']['handler_settings']['behaviors'] : array();
  132. if (!empty($instance['settings']['behaviors'])) {
  133. $behaviors = array_merge($behaviors, $instance['settings']['behaviors']);
  134. }
  135. foreach ($behaviors as $behavior => $settings) {
  136. if (empty($settings['status'])) {
  137. // Behavior is not enabled.
  138. continue;
  139. }
  140. $object_cache[$identifier][] = _entityreference_get_behavior_handler($behavior);
  141. }
  142. }
  143. return $object_cache[$identifier];
  144. }
  145. /**
  146. * Get the behavior handler for a given entityreference field and instance.
  147. *
  148. * @param $handler
  149. * The behavior handler name.
  150. */
  151. function _entityreference_get_behavior_handler($behavior) {
  152. $object_cache = drupal_static(__FUNCTION__);
  153. if (!isset($object_cache[$behavior])) {
  154. ctools_include('plugins');
  155. $class = ctools_plugin_load_class('entityreference', 'behavior', $behavior, 'class');
  156. $class = class_exists($class) ? $class : 'EntityReference_BehaviorHandler_Broken';
  157. $object_cache[$behavior] = new $class($behavior);
  158. }
  159. return $object_cache[$behavior];
  160. }
  161. /**
  162. * Get the selection handler for a given entityreference field.
  163. */
  164. function entityreference_get_selection_handler($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
  165. ctools_include('plugins');
  166. $handler = $field['settings']['handler'];
  167. $class = ctools_plugin_load_class('entityreference', 'selection', $handler, 'class');
  168. if (class_exists($class)) {
  169. return call_user_func(array($class, 'getInstance'), $field, $instance, $entity_type, $entity);
  170. }
  171. else {
  172. return EntityReference_SelectionHandler_Broken::getInstance($field, $instance, $entity_type, $entity);
  173. }
  174. }
  175. /**
  176. * Implements hook_field_load().
  177. */
  178. function entityreference_field_load($entity_type, $entities, $field, $instances, $langcode, &$items) {
  179. // Invoke the behaviors.
  180. foreach (entityreference_get_behavior_handlers($field) as $handler) {
  181. $handler->load($entity_type, $entities, $field, $instances, $langcode, $items);
  182. }
  183. }
  184. /**
  185. * Implements hook_field_validate().
  186. */
  187. function entityreference_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  188. $ids = array();
  189. foreach ($items as $delta => $item) {
  190. if (!entityreference_field_is_empty($item, $field) && $item['target_id'] !== NULL) {
  191. $ids[$item['target_id']] = $delta;
  192. }
  193. }
  194. if ($ids) {
  195. $valid_ids = entityreference_get_selection_handler($field, $instance, $entity_type, $entity)->validateReferencableEntities(array_keys($ids));
  196. $invalid_entities = array_diff_key($ids, array_flip($valid_ids));
  197. if ($invalid_entities) {
  198. foreach ($invalid_entities as $id => $delta) {
  199. $errors[$field['field_name']][$langcode][$delta][] = array(
  200. 'error' => 'entityreference_invalid_entity',
  201. 'message' => t('The referenced entity (@type: @id) is invalid.', array('@type' => $field['settings']['target_type'], '@id' => $id)),
  202. );
  203. }
  204. }
  205. }
  206. // Invoke the behaviors.
  207. foreach (entityreference_get_behavior_handlers($field, $instance) as $handler) {
  208. $handler->validate($entity_type, $entity, $field, $instance, $langcode, $items, $errors);
  209. }
  210. }
  211. /**
  212. * Implements hook_field_presave().
  213. *
  214. * Adds the target type to the field data structure when saving.
  215. */
  216. function entityreference_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  217. // Invoke the behaviors.
  218. foreach (entityreference_get_behavior_handlers($field, $instance) as $handler) {
  219. $handler->presave($entity_type, $entity, $field, $instance, $langcode, $items);
  220. }
  221. }
  222. /**
  223. * Implements hook_field_insert().
  224. */
  225. function entityreference_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
  226. // Invoke the behaviors.
  227. foreach (entityreference_get_behavior_handlers($field, $instance) as $handler) {
  228. $handler->insert($entity_type, $entity, $field, $instance, $langcode, $items);
  229. }
  230. }
  231. /**
  232. * Implements hook_field_attach_insert().
  233. *
  234. * Emulates a post-insert hook.
  235. */
  236. function entityreference_field_attach_insert($entity_type, $entity) {
  237. list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  238. foreach (field_info_instances($entity_type, $bundle) as $field_name => $instance) {
  239. $field = field_info_field($field_name);
  240. if ($field['type'] == 'entityreference') {
  241. foreach (entityreference_get_behavior_handlers($field, $instance) as $handler) {
  242. $handler->postInsert($entity_type, $entity, $field, $instance);
  243. }
  244. }
  245. }
  246. }
  247. /**
  248. * Implements hook_field_update().
  249. */
  250. function entityreference_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
  251. // Invoke the behaviors.
  252. foreach (entityreference_get_behavior_handlers($field, $instance) as $handler) {
  253. $handler->update($entity_type, $entity, $field, $instance, $langcode, $items);
  254. }
  255. }
  256. /**
  257. * Implements hook_field_attach_update().
  258. *
  259. * Emulates a post-update hook.
  260. */
  261. function entityreference_field_attach_update($entity_type, $entity) {
  262. list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  263. foreach (field_info_instances($entity_type, $bundle) as $field_name => $instance) {
  264. $field = field_info_field($field_name);
  265. if ($field['type'] == 'entityreference') {
  266. foreach (entityreference_get_behavior_handlers($field, $instance) as $handler) {
  267. $handler->postUpdate($entity_type, $entity, $field, $instance);
  268. }
  269. }
  270. }
  271. }
  272. /**
  273. * Implements hook_field_delete().
  274. */
  275. function entityreference_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) {
  276. // Invoke the behaviors.
  277. foreach (entityreference_get_behavior_handlers($field, $instance) as $handler) {
  278. $handler->delete($entity_type, $entity, $field, $instance, $langcode, $items);
  279. }
  280. }
  281. /**
  282. * Implements hook_field_attach_delete().
  283. *
  284. * Emulates a post-delete hook.
  285. */
  286. function entityreference_field_attach_delete($entity_type, $entity) {
  287. list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  288. foreach (field_info_instances($entity_type, $bundle) as $field_name => $instance) {
  289. $field = field_info_field($field_name);
  290. if ($field['type'] == 'entityreference') {
  291. foreach (entityreference_get_behavior_handlers($field, $instance) as $handler) {
  292. $handler->postDelete($entity_type, $entity, $field, $instance);
  293. }
  294. }
  295. }
  296. }
  297. /**
  298. * Implements hook_entity_insert().
  299. */
  300. function entityreference_entity_insert($entity, $entity_type) {
  301. entityreference_entity_crud($entity, $entity_type, 'entityPostInsert');
  302. }
  303. /**
  304. * Implements hook_entity_update().
  305. */
  306. function entityreference_entity_update($entity, $entity_type) {
  307. entityreference_entity_crud($entity, $entity_type, 'entityPostUpdate');
  308. }
  309. /**
  310. * Implements hook_entity_delete().
  311. */
  312. function entityreference_entity_delete($entity, $entity_type) {
  313. entityreference_entity_crud($entity, $entity_type, 'entityPostDelete');
  314. }
  315. /**
  316. * Invoke a behavior based on entity CRUD.
  317. *
  318. * @param $entity
  319. * The entity object.
  320. * @param $entity_type
  321. * The entity type.
  322. * @param $method_name
  323. * The method to invoke.
  324. */
  325. function entityreference_entity_crud($entity, $entity_type, $method_name) {
  326. list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  327. foreach (field_info_instances($entity_type, $bundle) as $field_name => $instance) {
  328. $field = field_info_field($field_name);
  329. if ($field['type'] == 'entityreference') {
  330. foreach (entityreference_get_behavior_handlers($field, $instance) as $handler) {
  331. $handler->{$method_name}($entity_type, $entity, $field, $instance);
  332. }
  333. }
  334. }
  335. }
  336. /**
  337. * Implements hook_field_settings_form().
  338. */
  339. function entityreference_field_settings_form($field, $instance, $has_data) {
  340. // The field settings infrastructure is not AJAX enabled by default,
  341. // because it doesn't pass over the $form_state.
  342. // Build the whole form into a #process in which we actually have access
  343. // to the form state.
  344. $form = array(
  345. '#type' => 'container',
  346. '#attached' => array(
  347. 'css' => array(drupal_get_path('module', 'entityreference') . '/entityreference.admin.css'),
  348. ),
  349. '#process' => array(
  350. '_entityreference_field_settings_process',
  351. '_entityreference_field_settings_ajax_process',
  352. ),
  353. '#element_validate' => array('_entityreference_field_settings_validate'),
  354. '#field' => $field,
  355. '#instance' => $instance,
  356. '#has_data' => $has_data,
  357. );
  358. return $form;
  359. }
  360. function _entityreference_field_settings_process($form, $form_state) {
  361. $field = isset($form_state['entityreference']['field']) ? $form_state['entityreference']['field'] : $form['#field'];
  362. $instance = isset($form_state['entityreference']['instance']) ? $form_state['entityreference']['instance'] : $form['#instance'];
  363. $has_data = $form['#has_data'];
  364. $settings = $field['settings'];
  365. $settings += array('handler' => 'base');
  366. // Select the target entity type.
  367. $entity_type_options = array();
  368. foreach (entity_get_info() as $entity_type => $entity_info) {
  369. $entity_type_options[$entity_type] = $entity_info['label'];
  370. }
  371. $form['target_type'] = array(
  372. '#type' => 'select',
  373. '#title' => t('Target type'),
  374. '#options' => $entity_type_options,
  375. '#default_value' => $field['settings']['target_type'],
  376. '#required' => TRUE,
  377. '#description' => t('The entity type that can be referenced through this field.'),
  378. '#disabled' => $has_data,
  379. '#size' => 1,
  380. '#ajax' => TRUE,
  381. '#limit_validation_errors' => array(),
  382. );
  383. ctools_include('plugins');
  384. $handlers = ctools_get_plugins('entityreference', 'selection');
  385. uasort($handlers, 'ctools_plugin_sort');
  386. $handlers_options = array();
  387. foreach ($handlers as $handler => $handler_info) {
  388. $handlers_options[$handler] = check_plain($handler_info['title']);
  389. }
  390. $form['handler'] = array(
  391. '#type' => 'fieldset',
  392. '#title' => t('Entity selection'),
  393. '#tree' => TRUE,
  394. '#process' => array('_entityreference_form_process_merge_parent'),
  395. );
  396. $form['handler']['handler'] = array(
  397. '#type' => 'select',
  398. '#title' => t('Mode'),
  399. '#options' => $handlers_options,
  400. '#default_value' => $settings['handler'],
  401. '#required' => TRUE,
  402. '#ajax' => TRUE,
  403. '#limit_validation_errors' => array(),
  404. );
  405. $form['handler_submit'] = array(
  406. '#type' => 'submit',
  407. '#value' => t('Change handler'),
  408. '#limit_validation_errors' => array(),
  409. '#attributes' => array(
  410. 'class' => array('js-hide'),
  411. ),
  412. '#submit' => array('entityreference_settings_ajax_submit'),
  413. );
  414. $form['handler']['handler_settings'] = array(
  415. '#type' => 'container',
  416. '#attributes' => array('class' => array('entityreference-settings')),
  417. );
  418. $handler = entityreference_get_selection_handler($field, $instance);
  419. $form['handler']['handler_settings'] += $handler->settingsForm($field, $instance);
  420. _entityreference_get_behavior_elements($form, $field, $instance, 'field');
  421. if (!empty($form['behaviors'])) {
  422. $form['behaviors'] += array(
  423. '#type' => 'fieldset',
  424. '#title' => t('Additional behaviors'),
  425. '#parents' => array_merge($form['#parents'], array('handler_settings', 'behaviors')),
  426. );
  427. }
  428. return $form;
  429. }
  430. function _entityreference_field_settings_ajax_process($form, $form_state) {
  431. _entityreference_field_settings_ajax_process_element($form, $form);
  432. return $form;
  433. }
  434. function _entityreference_field_settings_ajax_process_element(&$element, $main_form) {
  435. if (isset($element['#ajax']) && $element['#ajax'] === TRUE) {
  436. $element['#ajax'] = array(
  437. 'callback' => 'entityreference_settings_ajax',
  438. 'wrapper' => $main_form['#id'],
  439. 'element' => $main_form['#array_parents'],
  440. );
  441. }
  442. foreach (element_children($element) as $key) {
  443. _entityreference_field_settings_ajax_process_element($element[$key], $main_form);
  444. }
  445. }
  446. function _entityreference_form_process_merge_parent($element) {
  447. $parents = $element['#parents'];
  448. array_pop($parents);
  449. $element['#parents'] = $parents;
  450. return $element;
  451. }
  452. function _entityreference_element_validate_filter(&$element, &$form_state) {
  453. $element['#value'] = array_filter($element['#value']);
  454. form_set_value($element, $element['#value'], $form_state);
  455. }
  456. function _entityreference_field_settings_validate($form, &$form_state) {
  457. // Store the new values in the form state.
  458. $field = $form['#field'];
  459. if (isset($form_state['values']['field'])) {
  460. $field['settings'] = $form_state['values']['field']['settings'];
  461. }
  462. $form_state['entityreference']['field'] = $field;
  463. unset($form_state['values']['field']['settings']['handler_submit']);
  464. }
  465. /**
  466. * Implements hook_field_instance_settings_form().
  467. */
  468. function entityreference_field_instance_settings_form($field, $instance) {
  469. $form['settings'] = array(
  470. '#type' => 'container',
  471. '#attached' => array(
  472. 'css' => array(drupal_get_path('module', 'entityreference') . '/entityreference.admin.css'),
  473. ),
  474. '#weight' => 10,
  475. '#tree' => TRUE,
  476. '#process' => array(
  477. '_entityreference_form_process_merge_parent',
  478. '_entityreference_field_instance_settings_form',
  479. '_entityreference_field_settings_ajax_process',
  480. ),
  481. '#element_validate' => array('_entityreference_field_instance_settings_validate'),
  482. '#field' => $field,
  483. '#instance' => $instance,
  484. );
  485. return $form;
  486. }
  487. function _entityreference_field_instance_settings_form($form, $form_state) {
  488. $field = isset($form_state['entityreference']['field']) ? $form_state['entityreference']['field'] : $form['#field'];
  489. $instance = isset($form_state['entityreference']['instance']) ? $form_state['entityreference']['instance'] : $form['#instance'];
  490. _entityreference_get_behavior_elements($form, $field, $instance, 'instance');
  491. if (!empty($form['behaviors'])) {
  492. $form['behaviors'] += array(
  493. '#type' => 'fieldset',
  494. '#title' => t('Additional behaviors'),
  495. '#process' => array(
  496. '_entityreference_field_settings_ajax_process',
  497. ),
  498. );
  499. }
  500. return $form;
  501. }
  502. function _entityreference_field_instance_settings_validate($form, &$form_state) {
  503. // Store the new values in the form state.
  504. $instance = $form['#instance'];
  505. if (isset($form_state['values']['instance'])) {
  506. $instance = drupal_array_merge_deep($instance, $form_state['values']['instance']);
  507. }
  508. $form_state['entityreference']['instance'] = $instance;
  509. }
  510. /**
  511. * Get the field or instance elements for the field configuration.
  512. */
  513. function _entityreference_get_behavior_elements(&$element, $field, $instance, $level) {
  514. // Add the accessible behavior handlers.
  515. $behavior_plugins = entityreference_get_accessible_behavior_plugins($field, $instance);
  516. if ($behavior_plugins[$level]) {
  517. $element['behaviors'] = array();
  518. foreach ($behavior_plugins[$level] as $name => $plugin) {
  519. if ($level == 'field') {
  520. $settings = !empty($field['settings']['handler_settings']['behaviors'][$name]) ? $field['settings']['handler_settings']['behaviors'][$name] : array();
  521. }
  522. else {
  523. $settings = !empty($instance['settings']['behaviors'][$name]) ? $instance['settings']['behaviors'][$name] : array();
  524. }
  525. $settings += array('status' => $plugin['force enabled']);
  526. // Render the checkbox.
  527. $element['behaviors'][$name] = array(
  528. '#tree' => TRUE,
  529. );
  530. $element['behaviors'][$name]['status'] = array(
  531. '#type' => 'checkbox',
  532. '#title' => check_plain($plugin['title']),
  533. '#description' => $plugin['description'],
  534. '#default_value' => $settings['status'],
  535. '#disabled' => $plugin['force enabled'],
  536. '#ajax' => TRUE,
  537. );
  538. if ($settings['status']) {
  539. $handler = _entityreference_get_behavior_handler($name);
  540. if ($behavior_elements = $handler->settingsForm($field, $instance)) {
  541. foreach ($behavior_elements as $key => &$behavior_element) {
  542. $behavior_element += array(
  543. '#default_value' => !empty($settings[$key]) ? $settings[$key] : NULL,
  544. );
  545. }
  546. // Get the behavior settings.
  547. $behavior_elements += array(
  548. '#type' => 'container',
  549. '#process' => array('_entityreference_form_process_merge_parent'),
  550. '#attributes' => array(
  551. 'class' => array('entityreference-settings'),
  552. ),
  553. );
  554. $element['behaviors'][$name]['settings'] = $behavior_elements;
  555. }
  556. }
  557. }
  558. }
  559. }
  560. /**
  561. * Get all accessible behavior plugins.
  562. */
  563. function entityreference_get_accessible_behavior_plugins($field, $instance) {
  564. ctools_include('plugins');
  565. $plugins = array('field' => array(), 'instance' => array());
  566. foreach (ctools_get_plugins('entityreference', 'behavior') as $name => $plugin) {
  567. $handler = _entityreference_get_behavior_handler($name);
  568. $level = $plugin['behavior type'];
  569. if ($handler->access($field, $instance)) {
  570. $plugins[$level][$name] = $plugin;
  571. }
  572. }
  573. return $plugins;
  574. }
  575. /**
  576. * Ajax callback for the handler settings form.
  577. *
  578. * @see entityreference_field_settings_form()
  579. */
  580. function entityreference_settings_ajax($form, $form_state) {
  581. $trigger = $form_state['triggering_element'];
  582. return drupal_array_get_nested_value($form, $trigger['#ajax']['element']);
  583. }
  584. /**
  585. * Submit handler for the non-JS case.
  586. *
  587. * @see entityreference_field_settings_form()
  588. */
  589. function entityreference_settings_ajax_submit($form, &$form_state) {
  590. $form_state['rebuild'] = TRUE;
  591. }
  592. /**
  593. * Property callback for the Entity Metadata framework.
  594. */
  595. function entityreference_field_property_callback(&$info, $entity_type, $field, $instance, $field_type) {
  596. // Set the property type based on the targe type.
  597. $field_type['property_type'] = $field['settings']['target_type'];
  598. // Then apply the default.
  599. entity_metadata_field_default_property_callback($info, $entity_type, $field, $instance, $field_type);
  600. // Invoke the behaviors to allow them to change the properties.
  601. foreach (entityreference_get_behavior_handlers($field, $instance) as $handler) {
  602. $handler->property_info_alter($info, $entity_type, $field, $instance, $field_type);
  603. }
  604. }
  605. /**
  606. * Implements hook_field_widget_info().
  607. */
  608. function entityreference_field_widget_info() {
  609. $widgets['entityreference_autocomplete'] = array(
  610. 'label' => t('Autocomplete'),
  611. 'description' => t('An autocomplete text field.'),
  612. 'field types' => array('entityreference'),
  613. 'settings' => array(
  614. 'match_operator' => 'CONTAINS',
  615. 'size' => 60,
  616. // We don't have a default here, because it's not the same between
  617. // the two widgets, and the Field API doesn't update default
  618. // settings when the widget changes.
  619. 'path' => '',
  620. ),
  621. );
  622. $widgets['entityreference_autocomplete_tags'] = array(
  623. 'label' => t('Autocomplete (Tags style)'),
  624. 'description' => t('An autocomplete text field.'),
  625. 'field types' => array('entityreference'),
  626. 'settings' => array(
  627. 'match_operator' => 'CONTAINS',
  628. 'size' => 60,
  629. // We don't have a default here, because it's not the same between
  630. // the two widgets, and the Field API doesn't update default
  631. // settings when the widget changes.
  632. 'path' => '',
  633. ),
  634. 'behaviors' => array(
  635. 'multiple values' => FIELD_BEHAVIOR_CUSTOM,
  636. ),
  637. );
  638. return $widgets;
  639. }
  640. /**
  641. * Implements hook_field_widget_info_alter().
  642. */
  643. function entityreference_field_widget_info_alter(&$info) {
  644. if (module_exists('options')) {
  645. $info['options_select']['field types'][] = 'entityreference';
  646. $info['options_buttons']['field types'][] = 'entityreference';
  647. }
  648. }
  649. /**
  650. * Implements hook_field_widget_settings_form().
  651. */
  652. function entityreference_field_widget_settings_form($field, $instance) {
  653. $widget = $instance['widget'];
  654. $settings = $widget['settings'] + field_info_widget_settings($widget['type']);
  655. $form = array();
  656. if ($widget['type'] == 'entityreference_autocomplete' || $widget['type'] == 'entityreference_autocomplete_tags') {
  657. $form['match_operator'] = array(
  658. '#type' => 'select',
  659. '#title' => t('Autocomplete matching'),
  660. '#default_value' => $settings['match_operator'],
  661. '#options' => array(
  662. 'STARTS_WITH' => t('Starts with'),
  663. 'CONTAINS' => t('Contains'),
  664. ),
  665. '#description' => t('Select the method used to collect autocomplete suggestions. Note that <em>Contains</em> can cause performance issues on sites with thousands of nodes.'),
  666. );
  667. $form['size'] = array(
  668. '#type' => 'textfield',
  669. '#title' => t('Size of textfield'),
  670. '#default_value' => $settings['size'],
  671. '#element_validate' => array('_element_validate_integer_positive'),
  672. '#required' => TRUE,
  673. );
  674. }
  675. return $form;
  676. }
  677. /**
  678. * Implements hook_options_list().
  679. */
  680. function entityreference_options_list($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
  681. if (!$options = entityreference_get_selection_handler($field, $instance, $entity_type, $entity)->getReferencableEntities()) {
  682. return array();
  683. }
  684. // Rebuild the array, by changing the bundle key into the bundle label.
  685. $target_type = $field['settings']['target_type'];
  686. $entity_info = entity_get_info($target_type);
  687. $return = array();
  688. foreach ($options as $bundle => $entity_ids) {
  689. $bundle_label = check_plain($entity_info['bundles'][$bundle]['label']);
  690. $return[$bundle_label] = $entity_ids;
  691. }
  692. return count($return) == 1 ? reset($return) : $return;
  693. }
  694. /**
  695. * Implements hook_query_TAG_alter().
  696. */
  697. function entityreference_query_entityreference_alter(QueryAlterableInterface $query) {
  698. $handler = $query->getMetadata('entityreference_selection_handler');
  699. $handler->entityFieldQueryAlter($query);
  700. }
  701. /**
  702. * Implements hook_field_widget_form().
  703. */
  704. function entityreference_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  705. // Ensure that the entity target type exists before displaying the widget.
  706. $entity_info = entity_get_info($field['settings']['target_type']);
  707. if (empty($entity_info)){
  708. return;
  709. }
  710. $entity_type = $instance['entity_type'];
  711. $entity = isset($element['#entity']) ? $element['#entity'] : NULL;
  712. $handler = entityreference_get_selection_handler($field, $instance, $entity_type, $entity);
  713. if ($instance['widget']['type'] == 'entityreference_autocomplete' || $instance['widget']['type'] == 'entityreference_autocomplete_tags') {
  714. if ($instance['widget']['type'] == 'entityreference_autocomplete') {
  715. // We let the Field API handles multiple values for us, only take
  716. // care of the one matching our delta.
  717. if (isset($items[$delta])) {
  718. $items = array($items[$delta]);
  719. }
  720. else {
  721. $items = array();
  722. }
  723. }
  724. $entity_ids = array();
  725. $entity_labels = array();
  726. // Build an array of entities ID.
  727. foreach ($items as $item) {
  728. $entity_ids[] = $item['target_id'];
  729. }
  730. // Load those entities and loop through them to extract their labels.
  731. $entities = entity_load($field['settings']['target_type'], $entity_ids);
  732. foreach ($entities as $entity_id => $entity_item) {
  733. $label = $handler->getLabel($entity_item);
  734. $key = "$label ($entity_id)";
  735. // Labels containing commas or quotes must be wrapped in quotes.
  736. if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) {
  737. $key = '"' . str_replace('"', '""', $key) . '"';
  738. }
  739. $entity_labels[] = $key;
  740. }
  741. // Prepare the autocomplete path.
  742. if (!empty($instance['widget']['settings']['path'])) {
  743. $autocomplete_path = $instance['widget']['settings']['path'];
  744. }
  745. else {
  746. $autocomplete_path = $instance['widget']['type'] == 'entityreference_autocomplete' ? 'entityreference/autocomplete/single' : 'entityreference/autocomplete/tags';
  747. }
  748. $autocomplete_path .= '/' . $field['field_name'] . '/' . $instance['entity_type'] . '/' . $instance['bundle'] . '/';
  749. // Use <NULL> as a placeholder in the URL when we don't have an entity.
  750. // Most webservers collapse two consecutive slashes.
  751. $id = 'NULL';
  752. if ($entity) {
  753. list($eid) = entity_extract_ids($entity_type, $entity);
  754. if ($eid) {
  755. $id = $eid;
  756. }
  757. }
  758. $autocomplete_path .= $id;
  759. if ($instance['widget']['type'] == 'entityreference_autocomplete') {
  760. $element += array(
  761. '#type' => 'textfield',
  762. '#maxlength' => 1024,
  763. '#default_value' => implode(', ', $entity_labels),
  764. '#autocomplete_path' => $autocomplete_path,
  765. '#size' => $instance['widget']['settings']['size'],
  766. '#element_validate' => array('_entityreference_autocomplete_validate'),
  767. );
  768. return array('target_id' => $element);
  769. }
  770. else {
  771. $element += array(
  772. '#type' => 'textfield',
  773. '#maxlength' => 1024,
  774. '#default_value' => implode(', ', $entity_labels),
  775. '#autocomplete_path' => $autocomplete_path,
  776. '#size' => $instance['widget']['settings']['size'],
  777. '#element_validate' => array('_entityreference_autocomplete_tags_validate'),
  778. );
  779. return $element;
  780. }
  781. }
  782. }
  783. function _entityreference_autocomplete_validate($element, &$form_state, $form) {
  784. // If a value was entered into the autocomplete...
  785. $value = '';
  786. if (!empty($element['#value'])) {
  787. // Take "label (entity id)', match the id from parenthesis.
  788. if (preg_match("/.+\((\d+)\)/", $element['#value'], $matches)) {
  789. $value = $matches[1];
  790. }
  791. else {
  792. // Try to get a match from the input string when the user didn't use the
  793. // autocomplete but filled in a value manually.
  794. $field = field_info_field($element['#field_name']);
  795. $handler = entityreference_get_selection_handler($field);
  796. $field_name = $element['#field_name'];
  797. $field = field_info_field($field_name);
  798. $instance = field_info_instance($element['#entity_type'], $field_name, $element['#bundle']);
  799. $handler = entityreference_get_selection_handler($field, $instance);
  800. $value = $handler->validateAutocompleteInput($element['#value'], $element, $form_state, $form);
  801. }
  802. }
  803. // Update the value of this element so the field can validate the product IDs.
  804. form_set_value($element, $value, $form_state);
  805. }
  806. function _entityreference_autocomplete_tags_validate($element, &$form_state, $form) {
  807. $value = array();
  808. // If a value was entered into the autocomplete...
  809. if (!empty($element['#value'])) {
  810. $entities = drupal_explode_tags($element['#value']);
  811. $value = array();
  812. foreach ($entities as $entity) {
  813. // Take "label (entity id)', match the id from parenthesis.
  814. if (preg_match("/.+\((\d+)\)/", $entity, $matches)) {
  815. $value[] = array(
  816. 'target_id' => $matches[1],
  817. );
  818. }
  819. else {
  820. // Try to get a match from the input string when the user didn't use the
  821. // autocomplete but filled in a value manually.
  822. $field = field_info_field($element['#field_name']);
  823. $handler = entityreference_get_selection_handler($field);
  824. $value[] = array(
  825. 'target_id' => $handler->validateAutocompleteInput($entity, $element, $form_state, $form),
  826. );
  827. }
  828. }
  829. }
  830. // Update the value of this element so the field can validate the product IDs.
  831. form_set_value($element, $value, $form_state);
  832. }
  833. /**
  834. * Implements hook_field_widget_error().
  835. */
  836. function entityreference_field_widget_error($element, $error) {
  837. form_error($element, $error['message']);
  838. }
  839. /**
  840. * Menu Access callback for the autocomplete widget.
  841. *
  842. * @param $type
  843. * The widget type (i.e. 'single' or 'tags').
  844. * @param $field_name
  845. * The name of the entity-reference field.
  846. * @param $entity_type
  847. * The entity type.
  848. * @param $bundle_name
  849. * The bundle name.
  850. * @return
  851. * True if user can access this menu item.
  852. */
  853. function entityreference_autocomplete_access_callback($type, $field_name, $entity_type, $bundle_name) {
  854. $field = field_info_field($field_name);
  855. $instance = field_info_instance($entity_type, $field_name, $bundle_name);
  856. if (!$field || !$instance || $field['type'] != 'entityreference' || !field_access('edit', $field, $entity_type)) {
  857. return FALSE;
  858. }
  859. return TRUE;
  860. }
  861. /**
  862. * Menu callback: autocomplete the label of an entity.
  863. *
  864. * @param $type
  865. * The widget type (i.e. 'single' or 'tags').
  866. * @param $field_name
  867. * The name of the entity-reference field.
  868. * @param $entity_type
  869. * The entity type.
  870. * @param $bundle_name
  871. * The bundle name.
  872. * @param $entity_id
  873. * Optional; The entity ID the entity-reference field is attached to.
  874. * Defaults to ''.
  875. * @param $string
  876. * The label of the entity to query by.
  877. */
  878. function entityreference_autocomplete_callback($type, $field_name, $entity_type, $bundle_name, $entity_id = '', $string = '') {
  879. // If the request has a '/' in the search text, then the menu system will have
  880. // split it into multiple arguments and $string will only be a partial. We want
  881. // to make sure we recover the intended $string.
  882. $args = func_get_args();
  883. // Shift off the $type, $field_name, $entity_type, $bundle_name, and $entity_id args.
  884. array_shift($args);
  885. array_shift($args);
  886. array_shift($args);
  887. array_shift($args);
  888. array_shift($args);
  889. $string = implode('/', $args);
  890. $field = field_info_field($field_name);
  891. $instance = field_info_instance($entity_type, $field_name, $bundle_name);
  892. return entityreference_autocomplete_callback_get_matches($type, $field, $instance, $entity_type, $entity_id, $string);
  893. }
  894. /**
  895. * Return JSON based on given field, instance and string.
  896. *
  897. * This function can be used by other modules that wish to pass a mocked
  898. * definition of the field on instance.
  899. *
  900. * @param $type
  901. * The widget type (i.e. 'single' or 'tags').
  902. * @param $field
  903. * The field array defintion.
  904. * @param $instance
  905. * The instance array defintion.
  906. * @param $entity_type
  907. * The entity type.
  908. * @param $entity_id
  909. * Optional; The entity ID the entity-reference field is attached to.
  910. * Defaults to ''.
  911. * @param $string
  912. * The label of the entity to query by.
  913. */
  914. function entityreference_autocomplete_callback_get_matches($type, $field, $instance, $entity_type, $entity_id = '', $string = '') {
  915. $matches = array();
  916. $entity = NULL;
  917. if ($entity_id !== 'NULL') {
  918. $entity = entity_load_single($entity_type, $entity_id);
  919. $has_view_access = (entity_access('view', $entity_type, $entity) !== FALSE);
  920. $has_update_access = (entity_access('update', $entity_type, $entity) !== FALSE);
  921. if (!$entity || !($has_view_access || $has_update_access)) {
  922. return MENU_ACCESS_DENIED;
  923. }
  924. }
  925. $handler = entityreference_get_selection_handler($field, $instance, $entity_type, $entity);
  926. if ($type == 'tags') {
  927. // The user enters a comma-separated list of tags. We only autocomplete the last tag.
  928. $tags_typed = drupal_explode_tags($string);
  929. $tag_last = drupal_strtolower(array_pop($tags_typed));
  930. if (!empty($tag_last)) {
  931. $prefix = count($tags_typed) ? implode(', ', $tags_typed) . ', ' : '';
  932. }
  933. }
  934. else {
  935. // The user enters a single tag.
  936. $prefix = '';
  937. $tag_last = $string;
  938. }
  939. if (isset($tag_last)) {
  940. // Get an array of matching entities.
  941. $entity_labels = $handler->getReferencableEntities($tag_last, $instance['widget']['settings']['match_operator'], 10);
  942. // Loop through the products and convert them into autocomplete output.
  943. foreach ($entity_labels as $values) {
  944. foreach ($values as $entity_id => $label) {
  945. $key = "$label ($entity_id)";
  946. // Strip things like starting/trailing white spaces, line breaks and tags.
  947. $key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(decode_entities(strip_tags($key)))));
  948. // Names containing commas or quotes must be wrapped in quotes.
  949. if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) {
  950. $key = '"' . str_replace('"', '""', $key) . '"';
  951. }
  952. $matches[$prefix . $key] = '<div class="reference-autocomplete">' . $label . '</div>';
  953. }
  954. }
  955. }
  956. drupal_json_output($matches);
  957. }
  958. /**
  959. * Implements hook_field_formatter_info().
  960. */
  961. function entityreference_field_formatter_info() {
  962. return array(
  963. 'entityreference_label' => array(
  964. 'label' => t('Label'),
  965. 'description' => t('Display the label of the referenced entities.'),
  966. 'field types' => array('entityreference'),
  967. 'settings' => array(
  968. 'link' => FALSE,
  969. ),
  970. ),
  971. 'entityreference_entity_id' => array(
  972. 'label' => t('Entity id'),
  973. 'description' => t('Display the id of the referenced entities.'),
  974. 'field types' => array('entityreference'),
  975. ),
  976. 'entityreference_entity_view' => array(
  977. 'label' => t('Rendered entity'),
  978. 'description' => t('Display the referenced entities rendered by entity_view().'),
  979. 'field types' => array('entityreference'),
  980. 'settings' => array(
  981. 'view_mode' => 'default',
  982. 'links' => TRUE,
  983. ),
  984. ),
  985. );
  986. }
  987. /**
  988. * Implements hook_field_formatter_settings_form().
  989. */
  990. function entityreference_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  991. $display = $instance['display'][$view_mode];
  992. $settings = $display['settings'];
  993. if ($display['type'] == 'entityreference_label') {
  994. $element['link'] = array(
  995. '#title' => t('Link label to the referenced entity'),
  996. '#type' => 'checkbox',
  997. '#default_value' => $settings['link'],
  998. );
  999. }
  1000. if ($display['type'] == 'entityreference_entity_view') {
  1001. $entity_info = entity_get_info($field['settings']['target_type']);
  1002. $options = array('default' => t('Default'));
  1003. if (!empty($entity_info['view modes'])) {
  1004. foreach ($entity_info['view modes'] as $view_mode => $view_mode_settings) {
  1005. $options[$view_mode] = $view_mode_settings['label'];
  1006. }
  1007. }
  1008. $element['view_mode'] = array(
  1009. '#type' => 'select',
  1010. '#options' => $options,
  1011. '#title' => t('View mode'),
  1012. '#default_value' => $settings['view_mode'],
  1013. '#access' => count($options) > 1,
  1014. );
  1015. $element['links'] = array(
  1016. '#type' => 'checkbox',
  1017. '#title' => t('Show links'),
  1018. '#default_value' => $settings['links'],
  1019. );
  1020. }
  1021. return $element;
  1022. }
  1023. /**
  1024. * Implements hook_field_formatter_settings_summary().
  1025. */
  1026. function entityreference_field_formatter_settings_summary($field, $instance, $view_mode) {
  1027. $display = $instance['display'][$view_mode];
  1028. $settings = $display['settings'];
  1029. $summary = array();
  1030. if ($display['type'] == 'entityreference_label') {
  1031. $summary[] = $settings['link'] ? t('Link to the referenced entity') : t('No link');
  1032. }
  1033. if ($display['type'] == 'entityreference_entity_view') {
  1034. $entity_info = entity_get_info($field['settings']['target_type']);
  1035. $view_mode_label = $settings['view_mode'] == 'default' ? t('Default') : $settings['view_mode'];
  1036. if (isset($entity_info['view modes'][$settings['view_mode']]['label'])) {
  1037. $view_mode_label = $entity_info['view modes'][$settings['view_mode']]['label'];
  1038. }
  1039. $summary[] = t('Rendered as @mode', array('@mode' => $view_mode_label));
  1040. $summary[] = !empty($settings['links']) ? t('Display links') : t('Do not display links');
  1041. }
  1042. return implode('<br />', $summary);
  1043. }
  1044. /**
  1045. * Implements hook_field_formatter_prepare_view().
  1046. */
  1047. function entityreference_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
  1048. $target_ids = array();
  1049. // Collect every possible entity attached to any of the entities.
  1050. foreach ($entities as $id => $entity) {
  1051. foreach ($items[$id] as $delta => $item) {
  1052. if (isset($item['target_id'])) {
  1053. $target_ids[] = $item['target_id'];
  1054. }
  1055. }
  1056. }
  1057. if ($target_ids) {
  1058. $target_entities = entity_load($field['settings']['target_type'], $target_ids);
  1059. }
  1060. else {
  1061. $target_entities = array();
  1062. }
  1063. // Iterate through the fieldable entities again to attach the loaded data.
  1064. foreach ($entities as $id => $entity) {
  1065. $rekey = FALSE;
  1066. foreach ($items[$id] as $delta => $item) {
  1067. // Check whether the referenced entity could be loaded.
  1068. if (isset($target_entities[$item['target_id']])) {
  1069. // Replace the instance value with the term data.
  1070. $items[$id][$delta]['entity'] = $target_entities[$item['target_id']];
  1071. // Check whether the user has access to the referenced entity.
  1072. $has_view_access = (entity_access('view', $field['settings']['target_type'], $target_entities[$item['target_id']]) !== FALSE);
  1073. $has_update_access = (entity_access('update', $field['settings']['target_type'], $target_entities[$item['target_id']]) !== FALSE);
  1074. $items[$id][$delta]['access'] = ($has_view_access || $has_update_access);
  1075. }
  1076. // Otherwise, unset the instance value, since the entity does not exist.
  1077. else {
  1078. unset($items[$id][$delta]);
  1079. $rekey = TRUE;
  1080. }
  1081. }
  1082. if ($rekey) {
  1083. // Rekey the items array.
  1084. $items[$id] = array_values($items[$id]);
  1085. }
  1086. }
  1087. }
  1088. /**
  1089. * Implements hook_field_formatter_view().
  1090. */
  1091. function entityreference_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  1092. $result = array();
  1093. $settings = $display['settings'];
  1094. // Rebuild the items list to contain only those with access.
  1095. foreach ($items as $key => $item) {
  1096. if (empty($item['access'])) {
  1097. unset($items[$key]);
  1098. }
  1099. }
  1100. switch ($display['type']) {
  1101. case 'entityreference_label':
  1102. $handler = entityreference_get_selection_handler($field, $instance, $entity_type, $entity);
  1103. foreach ($items as $delta => $item) {
  1104. $label = $handler->getLabel($item['entity']);
  1105. // If the link is to be displayed and the entity has a uri, display a link.
  1106. // Note the assignment ($url = ) here is intended to be an assignment.
  1107. if ($display['settings']['link'] && ($uri = entity_uri($field['settings']['target_type'], $item['entity']))) {
  1108. $result[$delta] = array('#markup' => l($label, $uri['path'], $uri['options']));
  1109. }
  1110. else {
  1111. $result[$delta] = array('#markup' => check_plain($label));
  1112. }
  1113. }
  1114. break;
  1115. case 'entityreference_entity_id':
  1116. foreach ($items as $delta => $item) {
  1117. $result[$delta] = array('#markup' => check_plain($item['target_id']));
  1118. }
  1119. break;
  1120. case 'entityreference_entity_view':
  1121. foreach ($items as $delta => $item) {
  1122. // Protect ourselves from recursive rendering.
  1123. static $depth = 0;
  1124. $depth++;
  1125. if ($depth > 20) {
  1126. throw new EntityReferenceRecursiveRenderingException(t('Recursive rendering detected when rendering entity @entity_type(@entity_id). Aborting rendering.', array('@entity_type' => $entity_type, '@entity_id' => $item['target_id'])));
  1127. }
  1128. $entity = clone $item['entity'];
  1129. unset($entity->content);
  1130. $result[$delta] = entity_view($field['settings']['target_type'], array($item['target_id'] => $entity), $settings['view_mode'], $langcode, FALSE);
  1131. if (empty($settings['links']) && isset($result[$delta][$field['settings']['target_type']][$item['target_id']]['links'])) {
  1132. $result[$delta][$field['settings']['target_type']][$item['target_id']]['links']['#access'] = FALSE;
  1133. }
  1134. $depth = 0;
  1135. }
  1136. break;
  1137. }
  1138. return $result;
  1139. }
  1140. /**
  1141. * Exception thrown when the entity view renderer goes into a potentially infinite loop.
  1142. */
  1143. class EntityReferenceRecursiveRenderingException extends Exception {}
  1144. /**
  1145. * Implements hook_views_api().
  1146. */
  1147. function entityreference_views_api() {
  1148. return array(
  1149. 'api' => 3,
  1150. 'path' => drupal_get_path('module', 'entityreference') . '/views',
  1151. );
  1152. }
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.