entityreference_plugin_style.inc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * @file
  4. * Handler for entityreference_plugin_style.
  5. */
  6. class entityreference_plugin_style extends views_plugin_style {
  7. function option_definition() {
  8. $options = parent::option_definition();
  9. $options['search_fields'] = array('default' => NULL);
  10. return $options;
  11. }
  12. // Create the options form.
  13. function options_form(&$form, &$form_state) {
  14. parent::options_form($form, $form_state);
  15. $options = array();
  16. if (isset($form['grouping'])) {
  17. $options = $form['grouping'][0]['field']['#options'];
  18. unset($options['']);
  19. $form['search_fields'] = array(
  20. '#type' => 'checkboxes',
  21. '#title' => t('Search fields'),
  22. '#options' => $options,
  23. '#required' => TRUE,
  24. '#default_value' => isset($this->options['search_fields']) ? $this->options['search_fields'] : array(),
  25. '#description' => t('Select the field(s) that will be searched when using the autocomplete widget.'),
  26. '#weight' => -3,
  27. );
  28. }
  29. }
  30. function render() {
  31. $options = $this->display->handler->get_option('entityreference_options');
  32. // Play nice with Views UI 'preview' : if the view is not executed through
  33. // EntityReference_SelectionHandler_Views::getReferencableEntities(), just
  34. // display the HTML.
  35. if (empty($options)) {
  36. return parent::render();
  37. }
  38. // Group the rows according to the grouping field, if specified.
  39. $sets = $this->render_grouping($this->view->result, $this->options['grouping']);
  40. // Grab the alias of the 'id' field added by entityreference_plugin_display.
  41. $id_field_alias = $this->display->handler->id_field_alias;
  42. // @todo We don't display grouping info for now. Could be useful for select
  43. // widget, though.
  44. $results = array();
  45. foreach ($sets as $records) {
  46. foreach ($records as $index => $values) {
  47. $this->view->row_index = $index;
  48. // Sanitize html, remove line breaks and extra whitespace.
  49. $results[$values->{$id_field_alias}] = filter_xss_admin(preg_replace('/\s\s+/', ' ', str_replace("\n", '', $this->row_plugin->render($values))));
  50. }
  51. }
  52. unset($this->view->row_index);
  53. return $results;
  54. }
  55. }