entityreference.module 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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. $entity_type = $instance['entity_type'];
  706. $entity = isset($element['#entity']) ? $element['#entity'] : NULL;
  707. $handler = entityreference_get_selection_handler($field, $instance, $entity_type, $entity);
  708. if ($instance['widget']['type'] == 'entityreference_autocomplete' || $instance['widget']['type'] == 'entityreference_autocomplete_tags') {
  709. if ($instance['widget']['type'] == 'entityreference_autocomplete') {
  710. // We let the Field API handles multiple values for us, only take
  711. // care of the one matching our delta.
  712. if (isset($items[$delta])) {
  713. $items = array($items[$delta]);
  714. }
  715. else {
  716. $items = array();
  717. }
  718. }
  719. $entity_ids = array();
  720. $entity_labels = array();
  721. // Build an array of entities ID.
  722. foreach ($items as $item) {
  723. $entity_ids[] = $item['target_id'];
  724. }
  725. // Load those entities and loop through them to extract their labels.
  726. $entities = entity_load($field['settings']['target_type'], $entity_ids);
  727. foreach ($entities as $entity_id => $entity_item) {
  728. $label = $handler->getLabel($entity_item);
  729. $key = "$label ($entity_id)";
  730. // Labels containing commas or quotes must be wrapped in quotes.
  731. if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) {
  732. $key = '"' . str_replace('"', '""', $key) . '"';
  733. }
  734. $entity_labels[] = $key;
  735. }
  736. // Prepare the autocomplete path.
  737. if (!empty($instance['widget']['settings']['path'])) {
  738. $autocomplete_path = $instance['widget']['settings']['path'];
  739. }
  740. else {
  741. $autocomplete_path = $instance['widget']['type'] == 'entityreference_autocomplete' ? 'entityreference/autocomplete/single' : 'entityreference/autocomplete/tags';
  742. }
  743. $autocomplete_path .= '/' . $field['field_name'] . '/' . $instance['entity_type'] . '/' . $instance['bundle'] . '/';
  744. // Use <NULL> as a placeholder in the URL when we don't have an entity.
  745. // Most webservers collapse two consecutive slashes.
  746. $id = 'NULL';
  747. if ($entity) {
  748. list($eid) = entity_extract_ids($entity_type, $entity);
  749. if ($eid) {
  750. $id = $eid;
  751. }
  752. }
  753. $autocomplete_path .= $id;
  754. if ($instance['widget']['type'] == 'entityreference_autocomplete') {
  755. $element += array(
  756. '#type' => 'textfield',
  757. '#maxlength' => 1024,
  758. '#default_value' => implode(', ', $entity_labels),
  759. '#autocomplete_path' => $autocomplete_path,
  760. '#size' => $instance['widget']['settings']['size'],
  761. '#element_validate' => array('_entityreference_autocomplete_validate'),
  762. );
  763. return array('target_id' => $element);
  764. }
  765. else {
  766. $element += array(
  767. '#type' => 'textfield',
  768. '#maxlength' => 1024,
  769. '#default_value' => implode(', ', $entity_labels),
  770. '#autocomplete_path' => $autocomplete_path,
  771. '#size' => $instance['widget']['settings']['size'],
  772. '#element_validate' => array('_entityreference_autocomplete_tags_validate'),
  773. );
  774. return $element;
  775. }
  776. }
  777. }
  778. function _entityreference_autocomplete_validate($element, &$form_state, $form) {
  779. // If a value was entered into the autocomplete...
  780. $value = '';
  781. if (!empty($element['#value'])) {
  782. // Take "label (entity id)', match the id from parenthesis.
  783. if (preg_match("/.+\((\d+)\)/", $element['#value'], $matches)) {
  784. $value = $matches[1];
  785. }
  786. else {
  787. // Try to get a match from the input string when the user didn't use the
  788. // autocomplete but filled in a value manually.
  789. $field = field_info_field($element['#field_name']);
  790. $handler = entityreference_get_selection_handler($field);
  791. $field_name = $element['#field_name'];
  792. $field = field_info_field($field_name);
  793. $instance = field_info_instance($element['#entity_type'], $field_name, $element['#bundle']);
  794. $handler = entityreference_get_selection_handler($field, $instance);
  795. $value = $handler->validateAutocompleteInput($element['#value'], $element, $form_state, $form);
  796. }
  797. }
  798. // Update the value of this element so the field can validate the product IDs.
  799. form_set_value($element, $value, $form_state);
  800. }
  801. function _entityreference_autocomplete_tags_validate($element, &$form_state, $form) {
  802. $value = array();
  803. // If a value was entered into the autocomplete...
  804. if (!empty($element['#value'])) {
  805. $entities = drupal_explode_tags($element['#value']);
  806. $value = array();
  807. foreach ($entities as $entity) {
  808. // Take "label (entity id)', match the id from parenthesis.
  809. if (preg_match("/.+\((\d+)\)/", $entity, $matches)) {
  810. $value[] = array(
  811. 'target_id' => $matches[1],
  812. );
  813. }
  814. else {
  815. // Try to get a match from the input string when the user didn't use the
  816. // autocomplete but filled in a value manually.
  817. $field = field_info_field($element['#field_name']);
  818. $handler = entityreference_get_selection_handler($field);
  819. $value[] = array(
  820. 'target_id' => $handler->validateAutocompleteInput($entity, $element, $form_state, $form),
  821. );
  822. }
  823. }
  824. }
  825. // Update the value of this element so the field can validate the product IDs.
  826. form_set_value($element, $value, $form_state);
  827. }
  828. /**
  829. * Implements hook_field_widget_error().
  830. */
  831. function entityreference_field_widget_error($element, $error) {
  832. form_error($element, $error['message']);
  833. }
  834. /**
  835. * Menu Access callback for the autocomplete widget.
  836. *
  837. * @param $type
  838. * The widget type (i.e. 'single' or 'tags').
  839. * @param $field_name
  840. * The name of the entity-reference field.
  841. * @param $entity_type
  842. * The entity type.
  843. * @param $bundle_name
  844. * The bundle name.
  845. * @return
  846. * True if user can access this menu item.
  847. */
  848. function entityreference_autocomplete_access_callback($type, $field_name, $entity_type, $bundle_name) {
  849. $field = field_info_field($field_name);
  850. $instance = field_info_instance($entity_type, $field_name, $bundle_name);
  851. if (!$field || !$instance || $field['type'] != 'entityreference' || !field_access('edit', $field, $entity_type)) {
  852. return FALSE;
  853. }
  854. return TRUE;
  855. }
  856. /**
  857. * Menu callback: autocomplete the label of an entity.
  858. *
  859. * @param $type
  860. * The widget type (i.e. 'single' or 'tags').
  861. * @param $field_name
  862. * The name of the entity-reference field.
  863. * @param $entity_type
  864. * The entity type.
  865. * @param $bundle_name
  866. * The bundle name.
  867. * @param $entity_id
  868. * Optional; The entity ID the entity-reference field is attached to.
  869. * Defaults to ''.
  870. * @param $string
  871. * The label of the entity to query by.
  872. */
  873. function entityreference_autocomplete_callback($type, $field_name, $entity_type, $bundle_name, $entity_id = '', $string = '') {
  874. $field = field_info_field($field_name);
  875. $instance = field_info_instance($entity_type, $field_name, $bundle_name);
  876. return entityreference_autocomplete_callback_get_matches($type, $field, $instance, $entity_type, $entity_id, $string);
  877. }
  878. /**
  879. * Return JSON based on given field, instance and string.
  880. *
  881. * This function can be used by other modules that wish to pass a mocked
  882. * definition of the field on instance.
  883. *
  884. * @param $type
  885. * The widget type (i.e. 'single' or 'tags').
  886. * @param $field
  887. * The field array defintion.
  888. * @param $instance
  889. * The instance array defintion.
  890. * @param $entity_type
  891. * The entity type.
  892. * @param $entity_id
  893. * Optional; The entity ID the entity-reference field is attached to.
  894. * Defaults to ''.
  895. * @param $string
  896. * The label of the entity to query by.
  897. */
  898. function entityreference_autocomplete_callback_get_matches($type, $field, $instance, $entity_type, $entity_id = '', $string = '') {
  899. $matches = array();
  900. $entity = NULL;
  901. if ($entity_id !== 'NULL') {
  902. $entity = entity_load_single($entity_type, $entity_id);
  903. if (!$entity || !entity_access('view', $entity_type, $entity)) {
  904. return MENU_ACCESS_DENIED;
  905. }
  906. }
  907. $handler = entityreference_get_selection_handler($field, $instance, $entity_type, $entity);
  908. if ($type == 'tags') {
  909. // The user enters a comma-separated list of tags. We only autocomplete the last tag.
  910. $tags_typed = drupal_explode_tags($string);
  911. $tag_last = drupal_strtolower(array_pop($tags_typed));
  912. if (!empty($tag_last)) {
  913. $prefix = count($tags_typed) ? implode(', ', $tags_typed) . ', ' : '';
  914. }
  915. }
  916. else {
  917. // The user enters a single tag.
  918. $prefix = '';
  919. $tag_last = $string;
  920. }
  921. if (isset($tag_last)) {
  922. // Get an array of matching entities.
  923. $entity_labels = $handler->getReferencableEntities($tag_last, $instance['widget']['settings']['match_operator'], 10);
  924. // Loop through the products and convert them into autocomplete output.
  925. foreach ($entity_labels as $values) {
  926. foreach ($values as $entity_id => $label) {
  927. $key = "$label ($entity_id)";
  928. // Strip things like starting/trailing white spaces, line breaks and tags.
  929. $key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(decode_entities(strip_tags($key)))));
  930. // Names containing commas or quotes must be wrapped in quotes.
  931. if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) {
  932. $key = '"' . str_replace('"', '""', $key) . '"';
  933. }
  934. $matches[$prefix . $key] = '<div class="reference-autocomplete">' . $label . '</div>';
  935. }
  936. }
  937. }
  938. drupal_json_output($matches);
  939. }
  940. /**
  941. * Implements hook_field_formatter_info().
  942. */
  943. function entityreference_field_formatter_info() {
  944. return array(
  945. 'entityreference_label' => array(
  946. 'label' => t('Label'),
  947. 'description' => t('Display the label of the referenced entities.'),
  948. 'field types' => array('entityreference'),
  949. 'settings' => array(
  950. 'link' => FALSE,
  951. ),
  952. ),
  953. 'entityreference_entity_id' => array(
  954. 'label' => t('Entity id'),
  955. 'description' => t('Display the id of the referenced entities.'),
  956. 'field types' => array('entityreference'),
  957. ),
  958. 'entityreference_entity_view' => array(
  959. 'label' => t('Rendered entity'),
  960. 'description' => t('Display the referenced entities rendered by entity_view().'),
  961. 'field types' => array('entityreference'),
  962. 'settings' => array(
  963. 'view_mode' => '',
  964. 'links' => TRUE,
  965. ),
  966. ),
  967. );
  968. }
  969. /**
  970. * Implements hook_field_formatter_settings_form().
  971. */
  972. function entityreference_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  973. $display = $instance['display'][$view_mode];
  974. $settings = $display['settings'];
  975. if ($display['type'] == 'entityreference_label') {
  976. $element['link'] = array(
  977. '#title' => t('Link label to the referenced entity'),
  978. '#type' => 'checkbox',
  979. '#default_value' => $settings['link'],
  980. );
  981. }
  982. if ($display['type'] == 'entityreference_entity_view') {
  983. $entity_info = entity_get_info($field['settings']['target_type']);
  984. $options = array();
  985. if (!empty($entity_info['view modes'])) {
  986. foreach ($entity_info['view modes'] as $view_mode => $view_mode_settings) {
  987. $options[$view_mode] = $view_mode_settings['label'];
  988. }
  989. }
  990. if (count($options) > 1) {
  991. $element['view_mode'] = array(
  992. '#type' => 'select',
  993. '#options' => $options,
  994. '#title' => t('View mode'),
  995. '#default_value' => $settings['view_mode'],
  996. );
  997. }
  998. $element['links'] = array(
  999. '#type' => 'checkbox',
  1000. '#title' => t('Show links'),
  1001. '#default_value' => $settings['links'],
  1002. );
  1003. }
  1004. return $element;
  1005. }
  1006. /**
  1007. * Implements hook_field_formatter_settings_summary().
  1008. */
  1009. function entityreference_field_formatter_settings_summary($field, $instance, $view_mode) {
  1010. $display = $instance['display'][$view_mode];
  1011. $settings = $display['settings'];
  1012. $summary = array();
  1013. if ($display['type'] == 'entityreference_label') {
  1014. $summary[] = $settings['link'] ? t('Link to the referenced entity') : t('No link');
  1015. }
  1016. if ($display['type'] == 'entityreference_entity_view') {
  1017. $entity_info = entity_get_info($field['settings']['target_type']);
  1018. $summary[] = t('Rendered as @mode', array('@mode' => isset($entity_info['view modes'][$settings['view_mode']]['label']) ? $entity_info['view modes'][$settings['view_mode']]['label'] : $settings['view_mode']));
  1019. $summary[] = !empty($settings['links']) ? t('Display links') : t('Do not display links');
  1020. }
  1021. return implode('<br />', $summary);
  1022. }
  1023. /**
  1024. * Implements hook_field_formatter_prepare_view().
  1025. */
  1026. function entityreference_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
  1027. $target_ids = array();
  1028. // Collect every possible entity attached to any of the entities.
  1029. foreach ($entities as $id => $entity) {
  1030. foreach ($items[$id] as $delta => $item) {
  1031. if (isset($item['target_id'])) {
  1032. $target_ids[] = $item['target_id'];
  1033. }
  1034. }
  1035. }
  1036. if ($target_ids) {
  1037. $target_entities = entity_load($field['settings']['target_type'], $target_ids);
  1038. }
  1039. else {
  1040. $target_entities = array();
  1041. }
  1042. // Iterate through the fieldable entities again to attach the loaded data.
  1043. foreach ($entities as $id => $entity) {
  1044. $rekey = FALSE;
  1045. foreach ($items[$id] as $delta => $item) {
  1046. // Check whether the referenced entity could be loaded.
  1047. if (isset($target_entities[$item['target_id']])) {
  1048. // Replace the instance value with the term data.
  1049. $items[$id][$delta]['entity'] = $target_entities[$item['target_id']];
  1050. // Check whether the user has access to the referenced entity.
  1051. $items[$id][$delta]['access'] = entity_access('view', $field['settings']['target_type'], $target_entities[$item['target_id']]);
  1052. }
  1053. // Otherwise, unset the instance value, since the entity does not exist.
  1054. else {
  1055. unset($items[$id][$delta]);
  1056. $rekey = TRUE;
  1057. }
  1058. }
  1059. if ($rekey) {
  1060. // Rekey the items array.
  1061. $items[$id] = array_values($items[$id]);
  1062. }
  1063. }
  1064. }
  1065. /**
  1066. * Implements hook_field_formatter_view().
  1067. */
  1068. function entityreference_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  1069. $result = array();
  1070. $settings = $display['settings'];
  1071. // Rebuild the items list to contain only those with access.
  1072. foreach ($items as $key => $item) {
  1073. if (empty($item['access'])) {
  1074. unset($items[$key]);
  1075. }
  1076. }
  1077. switch ($display['type']) {
  1078. case 'entityreference_label':
  1079. $handler = entityreference_get_selection_handler($field, $instance, $entity_type, $entity);
  1080. foreach ($items as $delta => $item) {
  1081. $label = $handler->getLabel($item['entity']);
  1082. // If the link is to be displayed and the entity has a uri, display a link.
  1083. // Note the assignment ($url = ) here is intended to be an assignment.
  1084. if ($display['settings']['link'] && ($uri = entity_uri($field['settings']['target_type'], $item['entity']))) {
  1085. $result[$delta] = array('#markup' => l($label, $uri['path'], $uri['options']));
  1086. }
  1087. else {
  1088. $result[$delta] = array('#markup' => check_plain($label));
  1089. }
  1090. }
  1091. break;
  1092. case 'entityreference_entity_id':
  1093. foreach ($items as $delta => $item) {
  1094. $result[$delta] = array('#markup' => check_plain($item['target_id']));
  1095. }
  1096. break;
  1097. case 'entityreference_entity_view':
  1098. foreach ($items as $delta => $item) {
  1099. // Protect ourselves from recursive rendering.
  1100. static $depth = 0;
  1101. $depth++;
  1102. if ($depth > 20) {
  1103. 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'])));
  1104. }
  1105. $entity = clone $item['entity'];
  1106. unset($entity->content);
  1107. $result[$delta] = entity_view($field['settings']['target_type'], array($item['target_id'] => $entity), $settings['view_mode'], $langcode, FALSE);
  1108. if (empty($settings['links']) && isset($result[$delta][$field['settings']['target_type']][$item['target_id']]['links'])) {
  1109. $result[$delta][$field['settings']['target_type']][$item['target_id']]['links']['#access'] = FALSE;
  1110. }
  1111. $depth = 0;
  1112. }
  1113. break;
  1114. }
  1115. return $result;
  1116. }
  1117. /**
  1118. * Exception thrown when the entity view renderer goes into a potentially infinite loop.
  1119. */
  1120. class EntityReferenceRecursiveRenderingException extends Exception {}
  1121. /**
  1122. * Implements hook_views_api().
  1123. */
  1124. function entityreference_views_api() {
  1125. return array(
  1126. 'api' => 3,
  1127. 'path' => drupal_get_path('module', 'entityreference') . '/views',
  1128. );
  1129. }