publish_button_views_handler_node_link.inc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * @file
  4. * View handler for the module.
  5. */
  6. /**
  7. * Extends the node link handler.
  8. */
  9. class publish_button_views_handler_node_link extends views_handler_field_node_link {
  10. function construct() {
  11. parent::construct();
  12. $this->additional_fields['nid'] = 'nid';
  13. $this->additional_fields['status'] = 'status';
  14. }
  15. function option_definition() {
  16. $options = parent::option_definition();
  17. $options['text'] = array('default' => '', 'translatable' => TRUE);
  18. return $options;
  19. }
  20. function options_form(&$form, &$form_state) {
  21. parent::options_form($form, $form_state);
  22. // The default handler only have one text to display, unset to use two.
  23. unset($form['text']);
  24. $form['publish_button_publish'] = array(
  25. '#type' => 'textfield',
  26. '#title' => t('Text to display for publishing'),
  27. '#default_value' => $this->options['publish'],
  28. );
  29. $form['publish_button_unpublish'] = array(
  30. '#type' => 'textfield',
  31. '#title' => t('Text to display for unpublishing'),
  32. '#default_value' => $this->options['unpublish'],
  33. );
  34. }
  35. function query() {
  36. $this->ensure_my_table();
  37. $this->add_additional_fields();
  38. }
  39. function render($values) {
  40. $value = $this->get_value($values, 'nid');
  41. return $this->render_link($this->sanitize_value($value), $values);
  42. }
  43. function render_link($data, $values) {
  44. // Load the node to get the type, better programming needed...
  45. $node = node_load($data);
  46. if (_publish_button_publish_permissions($node->type) == TRUE && _publish_button_unpublish_permissions($node->type) == TRUE) {
  47. if ($values->node_status) {
  48. $status = 'unpublish';
  49. }
  50. if (!$values->node_status) {
  51. $status = 'publish';
  52. }
  53. if (isset($this->options['alter'])) {
  54. $csrf_token = drupal_get_token('publish_button_' . $data);
  55. $this->options['alter']['make_link'] = TRUE;
  56. $this->options['alter']['path'] = 'publish_button/' . $data . '/' . $status;
  57. $this->options['alter']['query'] = drupal_get_destination();
  58. $this->options['alter']['query']['csrf_token'] = $csrf_token;
  59. $text = empty($this->options[$status]) ? t($status) : $this->options[$status];
  60. $publish_button = array(
  61. '#type' => 'button',
  62. '#access' => TRUE,
  63. '#value' => $text ,
  64. '#weight' => '30',
  65. );
  66. return $publish_button;
  67. }
  68. }
  69. }
  70. }