entityreference.module 44 KB

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