publishcontent_views_handler_field_node_link.inc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * @file
  4. * Views field handler providing a publish, unpublish link.
  5. */
  6. /**
  7. * Field handler to present a link node publish.
  8. */
  9. class publishcontent_views_handler_field_node_link extends views_handler_field_node_link {
  10. /**
  11. * Define the field handler constructor.
  12. */
  13. function construct() {
  14. parent::construct();
  15. $this->additional_fields['nid'] = 'nid';
  16. $this->additional_fields['type'] = 'type';
  17. $this->additional_fields['status'] = 'status';
  18. $this->additional_fields['uid'] = 'uid';
  19. }
  20. /**
  21. * Define the view option definition.
  22. */
  23. function option_definition() {
  24. $options = parent::option_definition();
  25. $options['publish'] = $options['unpublish'] = array('default' => '', 'translatable' => TRUE);
  26. return $options;
  27. }
  28. /**
  29. * Define the view option form.
  30. */
  31. function options_form(&$form, &$form_state) {
  32. parent::options_form($form, $form_state);
  33. unset($form['text']);
  34. $form['publish'] = array(
  35. '#type' => 'textfield',
  36. '#title' => t('Text to display for publishing'),
  37. '#default_value' => $this->options['publish'],
  38. );
  39. $form['unpublish'] = array(
  40. '#type' => 'textfield',
  41. '#title' => t('Text to display for unpublishing'),
  42. '#default_value' => $this->options['unpublish'],
  43. );
  44. }
  45. /**
  46. * Define the fields query.
  47. */
  48. function query() {
  49. $this->ensure_my_table();
  50. $this->add_additional_fields();
  51. }
  52. /**
  53. * Render the field.
  54. */
  55. function render_link($node, $values) {
  56. // Ensure user has access to edit this node.
  57. if (!empty($node->status) && publishcontent_unpublish_access($node)) {
  58. $op = 'unpublish';
  59. }
  60. if (empty($node->status) && publishcontent_publish_access($node)) {
  61. $op = 'publish';
  62. }
  63. if (isset($op)) {
  64. $this->options['alter']['make_link'] = TRUE;
  65. $this->options['alter']['path'] = "node/$node->nid/$op/" . drupal_get_token();
  66. $this->options['alter']['query'] = drupal_get_destination();
  67. $this->options['alter']['link_class'] = 'publishcontent-link publishcontent-' . $op;
  68. return empty($this->options[$op]) ? t($op) : $this->options[$op];
  69. }
  70. }
  71. }