workflow.field.inc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * @file
  4. * Defines a Workflow field, widget and formatter. (copied from list field).
  5. */
  6. use Drupal\Core\Form\FormStateInterface;
  7. /**
  8. * Implements hook_form_FORM_ID_alter().
  9. *
  10. * Changes the hook_field_settings_form.
  11. * Fixes some Field settings and Field Instance settings, and makes sure users cannot change it.
  12. *
  13. * @todo: perhaps this is core functionality, but these values are only saved
  14. * when the site builder explicitly save the instance settings. :-(
  15. */
  16. function workflow_form_field_storage_config_edit_form_alter(&$form,
  17. FormStateInterface $form_state,
  18. /** @noinspection PhpUnusedParameterInspection */ $form_id) {
  19. /** @noinspection PhpUndefinedMethodInspection */
  20. $field_name = $form_state->getFormObject()->getEntity()->getType();
  21. if ($field_name == 'workflow') {
  22. // Make sure only 1 value can be entered in the Workflow field.
  23. $form['cardinality_container']['cardinality']['#default_value'] = 'number';
  24. $form['cardinality_container']['cardinality']['#disabled'] = TRUE;
  25. $form['cardinality_container']['cardinality_number']['#default_value'] = 1;
  26. $form['cardinality_container']['cardinality_number']['#disabled'] = TRUE;
  27. $form['cardinality_container']['cardinality_number']['#states'] = [];
  28. }
  29. }
  30. /**
  31. * Implements hook_form_FORM_ID_alter().
  32. */
  33. function workflow_form_field_config_edit_form_alter(&$form,
  34. FormStateInterface $form_state,
  35. /** @noinspection PhpUnusedParameterInspection */ $form_id) {
  36. /** @noinspection PhpUndefinedMethodInspection */
  37. $field_name = $form_state->getFormObject()->getEntity()->getType();
  38. if ($field_name == 'workflow') {
  39. // The Workflow field must have a value, so set to required.
  40. $form['required']['#default_value'] = 1;
  41. $form['required']['#disabled'] = TRUE;
  42. // There are alterations on the widget, too.
  43. // @see WorkflowDefaultWidget::formElement();
  44. }
  45. }
  46. /**
  47. * We will be using some default formatters and widgets from the List and Options modules.
  48. */
  49. /**
  50. * Implements hook_field_formatter_info_alter().
  51. *
  52. * The module reuses the formatters defined in list.module.
  53. */
  54. function workflow_field_formatter_info_alter(&$info) {
  55. $info['list_key']['field_types'][] = 'workflow';
  56. $info['list_default']['field_types'][] = 'workflow';
  57. }
  58. /**
  59. * Implements hook_field_widget_info_alter().
  60. *
  61. * The module does not implement widgets of its own, but reuses the
  62. * widgets defined in options.module.
  63. *
  64. * @see workflow_options_list()
  65. */
  66. function workflow_field_widget_info_alter(&$info) {
  67. $info['options_select']['field_types'][] = 'workflow';
  68. $info['options_buttons']['field_types'][] = 'workflow';
  69. }
  70. /**
  71. * Creates a form element to show the current value of a Workflow state.
  72. *
  73. * @param \Drupal\Core\Entity\EntityInterface $entity
  74. * The entity this field is on.
  75. * @param string $field_name
  76. * The field_name
  77. * @param string $current_sid
  78. * The current State Id.
  79. *
  80. * @return array
  81. * Form element, resembling the formatter of List module.
  82. * If state 0 is given, return an empty form element.
  83. */
  84. function workflow_state_formatter($entity, $field_name, $current_sid = '') {
  85. $element = [];
  86. if (!$current_sid) {
  87. $current_sid = workflow_node_current_state($entity, $field_name);
  88. }
  89. // If user creates a node, and only 1 option is available, the formatter
  90. // is shown with key, not value, because creation state does not count.
  91. // In this case, hide the formatter.
  92. /** @var \Drupal\workflow\Entity\WorkflowState $state */
  93. $state = \Drupal\workflow\Entity\WorkflowState::load($current_sid);
  94. if ($state->isCreationState()) {
  95. return $element;
  96. }
  97. // Clone the entity and restore old value, in case you want to show an
  98. // executed transition.
  99. if ($entity->{$field_name}->value != $current_sid) {
  100. $entity = clone $entity;
  101. $entity->{$field_name}->value = $current_sid;
  102. }
  103. // Generate a renderable array for the field. Use default language determination ($langcode = NULL).
  104. // First, add the 'current value' formatter for this field.
  105. // $list_display = $instance['display']['default'];
  106. $list_display['type'] = 'list_default';
  107. $element = $entity->{$field_name}->view($list_display);
  108. // @todo D8: make weight better (even better: hook_field_extra_fields).
  109. // Make sure the current value is before the form. (which has weight = 0.005)
  110. // $element['#weight'] = 0;
  111. return $element;
  112. }
  113. /**
  114. * This function is a residue for installations that have v8.x-1.0
  115. * The setting for options_allowed_values is still in the Field configuration
  116. * Saving the field data again does not remove the settings.
  117. * So, we copy code from options_allowed_values().
  118. *
  119. * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $definition
  120. *
  121. * @return mixed
  122. */
  123. function workflow_state_allowed_values(\Drupal\Core\Field\FieldStorageDefinitionInterface $definition) {
  124. $values = $definition->getSetting('allowed_values');
  125. return $values;
  126. }