workflow_state.inc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * @file
  4. * Describes a CTools Access plugin.
  5. *
  6. * Used to select whether or not a panel (variant) is displayed based
  7. * upon the current workflow and/or workflow state of the current node.
  8. * @see https://drupal.org/node/2187731
  9. */
  10. /**
  11. * Defines the Plugin.
  12. *
  13. * Plugins are described by creating a $plugin array which will
  14. * be used by the system that includes the file.
  15. */
  16. $plugin = array(
  17. 'title' => t('Workflow: state'),
  18. 'description' => t('Controls access by workflow bundle'),
  19. 'callback' => 'workflow_state_ctools_access_check',
  20. 'default' => array('workflow_state' => 0),
  21. 'settings form' => 'workflow_state_ctools_settings',
  22. 'summary' => 'workflow_state_ctools_summary',
  23. 'required context' => new ctools_context_required(t('Node'), 'node'),
  24. );
  25. /**
  26. * Custom callback defined by 'callback' in the $plugin array.
  27. *
  28. * Check for access.
  29. */
  30. function workflow_state_ctools_access_check($conf, $context) {
  31. // For some unknown reason $context may not be set. We just want to be sure.
  32. if (empty($context) || empty($context->data) || empty($context->data->workflow)) {
  33. return FALSE;
  34. }
  35. // If the node's content type is not part of the selected workflow access to
  36. // the pane is denied.
  37. $workflow_state = $context->data->workflow;
  38. if ($conf['workflow_state'] == $workflow_state) {
  39. return TRUE;
  40. }
  41. return FALSE;
  42. }
  43. /**
  44. * Settings form for the 'workflow state' access plugin.
  45. */
  46. function workflow_state_ctools_settings($form, &$form_state, $conf) {
  47. $options = array();
  48. $workflows = workflow_get_workflows();
  49. foreach ($workflows as $workflow) {
  50. $options[$workflow->name] = array();
  51. $states = workflow_get_workflow_states_by_wid($workflow->wid);
  52. foreach ($states as $state) {
  53. $options[$workflow->name][$state->sid] = $state->state;
  54. }
  55. }
  56. $form['settings']['workflow_state'] = array(
  57. '#title' => t('Select workflow state'),
  58. '#type' => 'select',
  59. '#options' => $options,
  60. '#description' => t('The pane will only be visible for nodes that are in this workflow state.'),
  61. '#default_value' => $conf['workflow_state'],
  62. );
  63. return $form;
  64. }
  65. /**
  66. * Provide a summary description based upon the workflow state.
  67. */
  68. function workflow_state_ctools_summary($conf, $context) {
  69. $state = workflow_get_workflow_states_by_sid($conf['workflow_state']);
  70. return t('Nodes that have the workflow state "@state" in workflow "@workflow"', array(
  71. '@state' => $state->state,
  72. '@workflow' => $state->name,
  73. )
  74. );
  75. }