/** * {@inheritdoc} */ public function post_execute($force = FALSE) { workflow_debug(__FILE__, __FUNCTION__, __LINE__); // @todo D8-port: still test this snippet. $state_changed = $from_sid != $to_sid; if ($state_changed || $this->getComment()) { $user = $this->getOwner(); \Drupal::moduleHandler()->invokeAll('workflow', ['transition post', $this, $user]); } }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { workflow_debug(__FILE__, __FUNCTION__, __LINE__); // @todo D8-port: still test this snippet. $form = parent::buildForm($form, $form_state); return $form; }
public function setWorkflow(Workflow $workflow) { workflow_debug(__FILE__, __FUNCTION__, __LINE__); // @todo D8-port: still test this snippet. $this->wid = $workflow->id(); $this->workflow = $workflow; }
/** * {@inheritdoc} */ public static function getPreviousStateId(EntityInterface $entity, $field_name = '') { $sid = ''; if (!$entity) { return $sid; } // If $field_name is not known, yet, determine it. $field_name = $field_name ? $field_name : workflow_get_field_name($entity, $field_name); // If $field_name is found, get more details. if (!$field_name) { // Return the initial value. return $sid; } if (isset($entity->original)) { // A changed node. workflow_debug(__FILE__, __FUNCTION__, __LINE__, $sid); // @todo D8-port: still test this snippet. } // A node may not have a Workflow attached. if ($entity->isNew()) { // A new Node. D7: $is_new is not set when saving terms, etc. $sid = self::getCreationStateId($entity, $field_name); } elseif (!$sid) { // @todo?: Read the history with an explicit langcode. $langcode = ''; // $entity->language()->getId(); $entity_type = $entity->getEntityTypeId(); if ($last_transition = WorkflowTransition::loadByProperties($entity_type, $entity->id(), [], $field_name, $langcode, 'DESC')) { $sid = $last_transition->getFromSid(); } } if (!$sid) { // No history found on an existing entity. $sid = self::getCreationStateId($entity, $field_name); } return $sid; }
/** * Generate an element. * * This function is referenced in the Annoteation for this class. */ public static function processTransition(&$element, FormStateInterface $form_state, &$complete_form) { workflow_debug(__FILE__, __FUNCTION__, __LINE__); // @todo D8-port: still test this snippet. return self::transitionElement($element, $form_state, $complete_form); }
function hook_field_widget_workflow_default_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) { // A hook specific for the 'workflow_default' widget. // D7: This hook is introduced in Drupal 7.8. // D8: This name is specified in the annotation of WorkflowDefaultWidget. workflow_debug(__FILE__, __FUNCTION__, __LINE__, '', ''); // A widget on an entity form. if ('workflow_default' == $context['widget']->getPluginId()) { // This object contains all you need. You may find it in one of two locations. /* @var $transition WorkflowTransitionInterface */ /* @var $transition WorkflowTransitionInterface */ $transition = $element['#default_value']; // An example of customizing/overriding the workflow widget. // Beware, until now, you must do this twice: on the widget and on the form. if ($transition->getOwnerId() == 1) { drupal_set_message('I got you, user 1, you will never schedule again, and you WILL document each state change!', 'warning'); // Let's prohibit scheduling for user 1. $element['workflow_scheduling']['#access'] = FALSE; // Let's prohibit scheduling for user 1. if ($element['comment']['#access'] == TRUE) { $element['comment']['#required'] = TRUE; } } } }
/** * {@inheritdoc} * * Be careful: Widget may be shown in very different places. Test carefully!! * - On a entity add/edit page * - On a entity preview page * - On a entity view page * - On a entity 'workflow history' tab * - On a comment display, in the comment history * - On a comment form, below the comment history * * @todo D8: change "array $items" to "FieldInterface $items" */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { $wid = $this->getFieldSetting('workflow_type'); if (!($workflow = Workflow::load($wid))) { // @todo: add error message. return $element; } /* @var $item \Drupal\workflowfield\Plugin\Field\FieldType\WorkflowItem */ $item = $items[$delta]; /* @var $field_config \Drupal\field\Entity\FieldConfig */ $field_config = $item->getFieldDefinition(); /* @var $field_storage \Drupal\field\Entity\FieldStorageConfig */ $field_storage = $field_config->getFieldStorageDefinition(); $field_name = $field_storage->get('field_name'); $entity = $item->getEntity(); $from_sid = workflow_node_current_state($entity, $field_name); // Create a transition, to pass to the form. No need to use setValues(). /* @var $transition WorkflowTransition */ $transition = WorkflowTransition::create([$from_sid, 'field_name' => $field_name]); $transition->setTargetEntity($entity); if (!$this->isDefaultValueWidget($form_state)) { // Here, not the $element is added, but the entity form. // Option 1: use the Element. $element['#default_value'] = $transition; $element += WorkflowTransitionElement::transitionElement($element, $form_state, $form); // Option 2: use the Form, in order to get extra fields. //$entity_form_builder = \Drupal::getContainer()->get('entity.form_builder'); //$element += $entity_form_builder->getForm($transition, 'add'); //// Remove the action button. The Entity itself has one. //unset($element['actions']); // Option 3: use the true Element. // $form = $this->element($form, $form_state, $transition); //$element['workflow_transition'] = array( // '#type' => 'workflow_transition', // '#title' => t('Workflow transition'), // '#default_value' => $transition, // ); } else { // @todo D8: add a default value, so people can set a default comment. // On the Field settings page, User may not set a default value // (this is done by the Workflow module). // @see WorkflowState::getOptions(); // @see WorkflowDefaultWidget::formElement(); $element = array(); return $element; workflow_debug(__FILE__, __FUNCTION__, __LINE__, '', ''); // @todo D8-port: still test this snippet. // @see workflowfield_form_field_config_edit_form_alter for other settings // The Workflow field must have a value, so set to required. // Unfortunately, we need hook_form_alter for this. //$form['required']['#default_value'] = 1; //$form['required']['#disabled'] = TRUE; // Explicitly set default value to 'creation' and show element, // so people can set a default comment. $transition->to_sid = $workflow->getCreationSid(); $transition->setExecuted(TRUE); // @TODO D8-port: use a proper WorkflowTransitionElement call. $element['#default_value'] = $transition; $element += WorkflowTransitionElement::transitionElement($element, $form_state, $form); // No action buttons on default field settings page. // This is evaluated in hook_form_alter. _workflow_use_action_buttons(FALSE); // Make sure the options box is not hidden (when action buttons active). //$element['to_sid']['#type'] = 'select'; $element['to_sid']['#title'] = 'Initial state'; $element['to_sid']['#access'] = TRUE; unset($element['workflow_current_state']); return $element; } return $element; }
/** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { foreach ($form_state->getValue($this->entitiesKey) as $sid => $value) { if (isset($this->entities[$sid])) { /* @var $state WorkflowState */ $state = $this->entities[$sid]; // Does user want to deactivate the state (reassign current content)? if ($sid && $value['status'] == 0 && $state->isActive()) { workflow_debug(__FILE__, __FUNCTION__, __LINE__, '', ''); // @todo D8-port: still test this snippet. $args = array('%state' => $state->label()); // Does that state have content in it? if ($value['count'] > 0 && empty($value['reassign'])) { if ($form['#last_mohican']) { $message = 'Since you are deleting the last available workflow state in this workflow, all content items which are in that state will have their workflow state removed.'; drupal_set_message($this->t($message, $args), 'warning'); } else { $message = 'The %state state has content; you must reassign the content to another state.'; $form_state->setErrorByName("states'][{$sid}]['reassign'", $this->t($message, $args)); } } } // Create the machine_name for new states. // N.B.: Keep machine_name in WorkflowState and ~ListBuillder aligned. if ($value['label_new'] && !$value['id']) { // $message = 'Machine name is required.'; // $form_state->setErrorByName('machine_name', $this->t($message)); } } } return; }
/** * {@inheritdoc} * * Saves a scheduled transition. If the transition is executed, save in history. */ public function save() { // If executed, save in history. if ($this->is_executed) { // Be careful, we are not a WorkflowScheduleTransition anymore! // No fuzzling around, just copy the ScheduledTransition to a normal one. $current_sid = $this->getFromSid(); $field_name = $this->getFieldName(); $executed_transition = WorkflowTransition::create([$current_sid, 'field_name' => $field_name]); $executed_transition->setTargetEntity($this->getTargetEntity()); $executed_transition->setValues($this->getToSid(), $this->getOwnerId(), REQUEST_TIME, $this->getComment()); return $executed_transition->save(); // <-- exit !! } $hid = $this->id(); if (!$hid) { // Insert the transition. Make sure it hasn't already been inserted. // @todo: Allow a scheduled transition per revision. $entity = $this->getTargetEntity(); $found_transition = self::loadByProperties($entity->getEntityTypeId(), $entity->id(), [], $this->getFieldName(), $this->getLangcode()); if ($found_transition) { // Avoid duplicate entries. $found_transition->delete(); $result = parent::save(); } else { $result = parent::save(); } } else { workflow_debug(__FILE__, __FUNCTION__, __LINE__); // @todo D8-port: still test this snippet. // Update the transition. $result = parent::save(); } // Create user message. if ($state = $this->getToState()) { $entity = $this->getTargetEntity(); $message = '%entity_title scheduled for state change to %state_name on %scheduled_date'; $args = array('%entity_title' => $entity->label(), '%state_name' => $state->label(), '%scheduled_date' => $this->getTimestampFormatted(), 'link' => $this->getTargetEntityId() ? $this->getTargetEntity()->link(t('View')) : ''); \Drupal::logger('workflow')->notice($message, $args); drupal_set_message(t($message, $args)); } return $result; }
/** * {@inheritdoc} */ protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) { workflow_debug(__FILE__, __FUNCTION__, __LINE__); // @todo D8-port: still test this snippet. return AccessResult::allowedIf($account->hasPermission('create ' . $entity_bundle . ' content'))->cachePerPermissions(); }