draggableviews_handler_fieldapi.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * @file
  4. * Field API handler plugin.
  5. */
  6. $plugin = array(
  7. 'label' => 'FieldAPI',
  8. 'handler' => array(
  9. 'class' => 'draggableviews_handler_fieldapi',
  10. ),
  11. );
  12. class draggableviews_handler_fieldapi extends draggableviews_handler {
  13. /**
  14. * Set default value of field option.
  15. */
  16. public function option_definition() {
  17. return array('field' => '');
  18. }
  19. /**
  20. * Add field options for handler.
  21. */
  22. function options_form($field) {
  23. $form = array();
  24. $options = array('' => t('- Select -'));
  25. // Check all the sortings added to a view. Hope there is
  26. // better way to determine them. Need to research.
  27. foreach ($field->view->display_handler->display->display_options['sorts'] as $sort_option) {
  28. $field_name = $sort_option['field'];
  29. // Field should be like "field_name_value".
  30. if (strpos($field_name, 'field_') === FALSE || strpos($field_name, '_value') === FALSE) {
  31. continue;
  32. }
  33. // Remove "_value" from field name and try to load the field.
  34. $field_name = drupal_substr($field_name, 0, drupal_strlen($field_name) - 6);
  35. if ($field_info = field_info_field($field_name)) {
  36. if ($field_info['type'] == 'number_integer') {
  37. $views_field_data = field_views_field_default_views_data($field_info);
  38. $options[$sort_option['table'] . ':' . $sort_option['field']] = filter_xss($views_field_data[$sort_option['table']][$sort_option['field']]['title']);
  39. }
  40. }
  41. }
  42. // If options are emtpy, show warning message.
  43. if (count($options) == 1) {
  44. $form['field_warning'] = array(
  45. '#markup' => '<div class="messages warning">' . t('Add weight integer field to sorts so it can be selected.') . '</div>',
  46. );
  47. }
  48. $form['field'] = array(
  49. '#type' => 'select',
  50. '#title' => t('Field'),
  51. '#options' => $options,
  52. '#default_value' => $field->options['draggableviews']['draggableviews_handler_fieldapi']['field'],
  53. '#description' => t('Please select field that contains weight. It should be integer type and already added to sorts of the view.'),
  54. );
  55. return $form;
  56. }
  57. function get($field, $index) {
  58. // Get the name of selected field.
  59. $field_option = $field->options['draggableviews']['draggableviews_handler_fieldapi']['field'];
  60. list($field_table, $field_name) = explode(':', $field_option);
  61. // Current row.
  62. $row = $field->view->result[$index];
  63. // Check whether key has table name and field name in it.
  64. foreach ($row as $key => $value) {
  65. if (strpos($key, $field_table) !== FALSE && strpos($key, $field_name) !== FALSE) {
  66. return $value;
  67. }
  68. }
  69. }
  70. function set($form_state) {
  71. $fv = $form_state['values'];
  72. $view = $form_state['build_info']['args'][0];
  73. $view_name = $view->name;
  74. $view_display = $view->current_display;
  75. // View arguments.
  76. $arguments = $view->args;
  77. if (isset($view->exposed_raw_input)) {
  78. $arguments += $view->exposed_raw_input;
  79. ksort($arguments);
  80. // Redirect view to the same page with exposed filters set.
  81. $form_state['redirect'] = array(current_path(), array('query' => $view->exposed_raw_input));
  82. }
  83. $base_table = $view->base_table;
  84. $entity_info_all = entity_get_info();
  85. $entity_type = '';
  86. foreach ($entity_info_all as $entity_name_key => $entity_info) {
  87. if ($entity_info['base table'] == $view->base_table) {
  88. $entity_type = $entity_name_key;
  89. break;
  90. }
  91. }
  92. $options_field = $view->field['draggableviews']->options['draggableviews']['draggableviews_handler_fieldapi']['field'];
  93. list($field_tabe, $field_column) = explode(':', $options_field);
  94. // Remove '_value' from column name to get field name.
  95. $field_name = drupal_substr($field_column, 0, drupal_strlen($field_column) - 6);
  96. // Save the values of the field.
  97. foreach ($fv['draggableviews'] as $item) {
  98. $entity_wrapper = entity_metadata_wrapper($entity_type, $item['id']);
  99. $entity_wrapper->{$field_name}->set($item['weight']);
  100. $entity_wrapper->save();
  101. }
  102. }
  103. }