flag_lists_handler_field_ops.inc 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. class flag_lists_handler_field_ops extends views_handler_field {
  3. function option_definition() {
  4. $options = parent::option_definition();
  5. $options['flo'] = array(
  6. 'contains' => array(
  7. 'force_single' => array('default' => FALSE),
  8. 'operation' => array('default' => 'flag'),
  9. ),
  10. );
  11. return $options;
  12. }
  13. function options_form(&$form, &$form_state) {
  14. parent::options_form($form, $form_state);
  15. $form['flo'] = array(
  16. '#type' => 'fieldset',
  17. '#title' => t('Flag lists operations'),
  18. '#collapsible' => FALSE,
  19. '#collapsed' => FALSE,
  20. );
  21. $form['flo']['force_single'] = array(
  22. '#type' => 'checkbox',
  23. '#title' => t('Force single'),
  24. '#default_value' => $this->options['flo']['force_single'],
  25. '#description' => t('Check this box to restrict selection to a single value.'),
  26. );
  27. $form['flo']['operation'] = array(
  28. '#type' => 'radios',
  29. '#title' => t('Operation'),
  30. '#default_value' => $this->options['flo']['operation'],
  31. '#description' => t('The flag lists operation for this selection.'),
  32. '#options' => array(
  33. 'flag' => 'Add to list',
  34. 'unflag' => 'Remove from list',
  35. ),
  36. '#required' => TRUE,
  37. );
  38. }
  39. /**
  40. * If the view is using a table style, provide a
  41. * placeholder for a "select all" checkbox.
  42. */
  43. function label() {
  44. if ($this->view->style_plugin instanceof views_plugin_style_table && !$this->options['flo']['force_single']) {
  45. return '<!--flag-lists-ops-select-all-->';
  46. }
  47. else {
  48. return parent::label();
  49. }
  50. }
  51. function render($values) {
  52. return '<!--form-item-' . $this->options['id'] . '--' . $this->view->row_index . '-->';
  53. }
  54. function views_form(&$form, &$form_state) {
  55. // The view is empty, abort.
  56. if (empty($this->view->result)) {
  57. return;
  58. }
  59. $form[$this->options['id']] = array(
  60. '#tree' => TRUE,
  61. );
  62. // At this point, the query has already been run, so we can access the results
  63. // in order to get the base key value (for example, nid for nodes).
  64. foreach ($this->view->result as $row_index => $row) {
  65. $entity_id = $this->get_value($row);
  66. if ($this->options['flo']['force_single']) {
  67. $form[$this->options['id']][$row_index] = array(
  68. '#type' => 'radio',
  69. '#parents' => array($this->options['id']),
  70. '#return_value' => $entity_id,
  71. );
  72. }
  73. else {
  74. $form[$this->options['id']][$row_index] = array(
  75. '#type' => 'checkbox',
  76. '#return_value' => $entity_id . (isset($row->flag_lists_flags_fid) ? ('-' . $row->flag_lists_flags_fid) : ''),
  77. '#default_value' => FALSE,
  78. '#attributes' => array('class' => array('flo-select')),
  79. );
  80. }
  81. }
  82. }
  83. }