entityreference.module 48 KB

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