entityreference.module 39 KB

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