draggableviews_handler_field_draggable.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * @file
  4. * Views field handler. Contains all relevant Draggableviews
  5. * options and related logic.
  6. * Implements the Views Form API.
  7. */
  8. class draggableviews_handler_field_draggable extends views_handler_field {
  9. function option_definition() {
  10. $options = parent::option_definition();
  11. $options['draggableviews'] = array(
  12. 'contains' => array(
  13. 'handler' => array('default' => 'draggableviews_handler_native'),
  14. 'save_button_label' => array('default' => 'Save'),
  15. 'ajax' => array('default' => FALSE),
  16. ),
  17. );
  18. // Populate default values of form elements provided by handlers.
  19. foreach (draggableviews_get_handlers() as $handler_id => $handler_object) {
  20. $options['draggableviews']['contains'][$handler_id] = array('default' => $handler_object->option_definition());
  21. }
  22. return $options;
  23. }
  24. function options_form(&$form, &$form_state) {
  25. parent::options_form($form, $form_state);
  26. // Remove not needed settings options.
  27. $form['alter']['#access'] = FALSE;
  28. $form['style_settings']['#access'] = FALSE;
  29. $form['empty_field_behavior']['#access'] = FALSE;
  30. $form['draggableviews'] = array(
  31. '#type' => 'fieldset',
  32. '#title' => t('Draggable Views'),
  33. '#collapsible' => TRUE,
  34. '#collapsed' => FALSE,
  35. );
  36. $handler_options = array();
  37. $handler_forms = array();
  38. foreach (draggableviews_get_handlers() as $handler_id => $handler_object) {
  39. $handler = ctools_get_plugins('draggableviews', 'handler', $handler_id);
  40. $handler_options[$handler_id] = filter_xss($handler['label']);
  41. $handler_forms[$handler_id] = $handler_object->options_form($this);
  42. }
  43. $form['draggableviews']['handler'] = array(
  44. '#type' => 'select',
  45. '#title' => t('Handler'),
  46. '#options' => $handler_options,
  47. '#default_value' => $this->options['draggableviews']['handler'],
  48. );
  49. // Add handler's form element as fielset that
  50. // is active only if handler selected.
  51. foreach ($handler_forms as $handler_id => $handler_form_element) {
  52. // Skip empty handler's form elements.
  53. if (empty($handler_form_element)) {
  54. continue;
  55. }
  56. $form['draggableviews'][$handler_id] = array(
  57. '#type' => 'fieldset',
  58. '#title' => check_plain($form['draggableviews']['handler']['#options'][$handler_id]),
  59. '#collapsible' => FALSE,
  60. '#states' => array(
  61. 'visible' => array(
  62. 'select[name="options[draggableviews][handler]"]' => array('value' => $handler_id),
  63. ),
  64. ),
  65. );
  66. foreach ($handler_form_element as $key => $form_element) {
  67. $form['draggableviews'][$handler_id][$key] = $form_element;
  68. }
  69. }
  70. $form['draggableviews']['save_button_label'] = array(
  71. '#type' => 'textfield',
  72. '#title' => t('Custom Save button label'),
  73. '#description' => t("Allow to change Save button Label."),
  74. '#default_value' => $this->options['draggableviews']['save_button_label'],
  75. );
  76. $form['draggableviews']['ajax'] = array(
  77. '#type' => 'checkbox',
  78. '#title' => t('Ajax'),
  79. '#description' => t('Use ajax in draggable form.'),
  80. '#default_value' => $this->options['draggableviews']['ajax'],
  81. );
  82. }
  83. function render($values) {
  84. if (user_access('access draggableviews')) {
  85. return '<!--form-item-' . $this->options['id'] . '--' . $this->view->row_index . '-->';
  86. }
  87. }
  88. /**
  89. * The form which replaces the placeholder from render().
  90. */
  91. function views_form(&$form, &$form_state) {
  92. // The view is empty, abort.
  93. if (empty($this->view->result)) {
  94. return;
  95. }
  96. $form[$this->options['id']] = array(
  97. '#tree' => TRUE,
  98. );
  99. $range = count($this->view->result);
  100. // At this point, the query has already been run, so we can access the results
  101. // in order to get the base key value (for example, nid for nodes).
  102. foreach ($this->view->result as $row_index => $row) {
  103. $entity_id = $this->get_value($row);
  104. $form[$this->options['id']][$row_index] = array(
  105. '#tree' => TRUE,
  106. );
  107. $handler_object = draggableviews_get_handler_class($this->options['draggableviews']['handler']);
  108. // Weight field selectbox.
  109. $form[$this->options['id']][$row_index]['weight'] = array(
  110. '#type' => 'select',
  111. '#options' => range(-$range, $range),
  112. '#attributes' => array('class' => array('draggableviews-weight')),
  113. '#default_value' => $handler_object->get($this, $row_index),
  114. );
  115. // Item to keep id of the entity.
  116. $form[$this->options['id']][$row_index]['id'] = array(
  117. '#type' => 'hidden',
  118. '#value' => $this->view->result[$row_index]->{$this->view->base_field},
  119. );
  120. }
  121. }
  122. }