views_send_handler_field_selector.inc 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * @file
  4. * Views field handler. Outputs a checkbox for selecting a row for email sending.
  5. * Implements the Views Form API.
  6. */
  7. class views_send_handler_field_selector extends views_handler_field {
  8. /**
  9. * If the view is using a table style, provide a
  10. * placeholder for a "select all" checkbox.
  11. */
  12. function label() {
  13. if ($this->view->style_plugin instanceof views_plugin_style_table) {
  14. return '<!--views-send-select-all-->';
  15. }
  16. else {
  17. return parent::label();
  18. }
  19. }
  20. function query() {
  21. // Do nothing.
  22. }
  23. function render($values) {
  24. return '<!--form-item-' . $this->options['id'] . '--' . $this->view->row_index . '-->';
  25. }
  26. /**
  27. * The form which replaces the placeholder from render().
  28. */
  29. function views_form(&$form, &$form_state) {
  30. // The view is empty, abort.
  31. if (empty($this->view->result)) {
  32. return;
  33. }
  34. $form[$this->options['id']] = array(
  35. '#tree' => TRUE,
  36. );
  37. foreach ($this->view->result as $row_index => $row) {
  38. $form[$this->options['id']][$row_index] = array(
  39. '#type' => 'checkbox',
  40. '#default_value' => FALSE,
  41. '#attributes' => array('class' => array('views-send-select')),
  42. );
  43. }
  44. }
  45. function views_form_validate($form, &$form_state) {
  46. $field_name = $this->options['id'];
  47. $selection = array_filter($form_state['values'][$field_name]);
  48. if (empty($selection)) {
  49. form_set_error($field_name, t('Please select at least one item.'));
  50. }
  51. }
  52. }