/** * {@inheritdoc} */ public function build() { $form = []; // Get the entity for this form. /* @var $entity EntityInterface */ if (!($entity = workflow_url_get_entity())) { return $form; } // Get the field name. Avoid error on Node Add page. if (!($field_name = workflow_get_field_name($entity))) { return $form; } /* * Output: generate the Transition Form. */ // Create a transition, to pass to the form. No need to use setValues(). $current_sid = workflow_node_current_state($entity, $field_name); $transition = WorkflowTransition::create([$current_sid, 'field_name' => $field_name]); $transition->setTargetEntity($entity); // Add the WorkflowTransitionForm to the page. $form = $this->entityFormBuilder()->getForm($transition, 'add'); return $form; }
/** * {@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; }
/** * @return WorkflowTransitionInterface */ protected function getTransitionForExecution(EntityInterface $entity) { $user = workflow_current_user(); if (!$entity) { \Drupal::logger('workflow_action')->notice('Unable to get current entity - entity is not defined.', []); return NULL; } // Get the entity type and numeric ID. $entity_id = $entity->id(); if (!$entity_id) { \Drupal::logger('workflow_action')->notice('Unable to get current entity ID - entity is not yet saved.', []); return NULL; } // In 'after saving new content', the node is already saved. Avoid second insert. // Todo: clone? $entity->enforceIsNew(FALSE); $config = $this->configuration; $field_name = workflow_get_field_name($entity, $config['field_name']); $current_sid = workflow_node_current_state($entity, $field_name); if (!$current_sid) { \Drupal::logger('workflow_action')->notice('Unable to get current workflow state of entity %id.', array('%id' => $entity_id)); return NULL; } $to_sid = isset($config['to_sid']) ? $config['to_sid'] : ''; // Get the Comment. Parse the $comment variables. $comment_string = $this->configuration['comment']; $comment = t($comment_string, array('%title' => $entity->label(), '%state' => workflow_get_sid_name($to_sid), '%user' => $user->getUsername())); $force = $this->configuration['force']; $transition = WorkflowTransition::create([$current_sid, 'field_name' => $field_name]); $transition->setTargetEntity($entity); $transition->setValues($to_sid, $user->id(), REQUEST_TIME, $comment); $transition->force($force); return $transition; }
/** * Generates an overview table of older revisions of a node, * but only if this::historyAccess() allows it. * * @param \Drupal\node\NodeInterface $node * A node object. * * @return array * An array as expected by drupal_render(). */ public function historyOverview(EntityInterface $node = NULL) { $form = array(); /* * Get data from parameters. */ // TODO D8-port: make Workflow History tab happen for every entity_type. // For workflow_tab_page with multiple workflows, use a separate view. See [#2217291]. // @see workflow.routing.yml, workflow.links.task.yml, WorkflowTransitionListController. // workflow_debug(__FILE__, __FUNCTION__, __LINE__); // @todo D8-port: still test this snippet. // ATM it only works for Nodes and Terms. // This is a hack. The Route should always pass an object. // On view tab, $entity is object, // On workflow tab, $entity is id(). // Get the entity for this form. if (!($entity = workflow_url_get_entity($node))) { return $form; } /* * Get derived data from parameters. */ if (!($field_name = workflow_get_field_name($entity, workflow_url_get_field_name()))) { return $form; } /* * Step 1: generate the Transition Form. */ // Create a transition, to pass to the form. No need to use setValues(). $current_sid = workflow_node_current_state($entity, $field_name); $transition = WorkflowTransition::create([$current_sid, 'field_name' => $field_name]); $transition->setTargetEntity($entity); // Add the WorkflowTransitionForm to the page. $form = $this->entityFormBuilder()->getForm($transition, 'add'); /* * Step 2: generate the Transition History List. */ $entity_type = 'workflow_transition'; // $form = $this->listing('workflow_transition'); $list_builder = $this->entityManager()->getListBuilder($entity_type); // Add the Node explicitly, since $list_builder expects a Transition. $list_builder->workflow_entity = $entity; $form += $list_builder->render(); /* * Finally: sort the elements (overriding their weight). */ // $form['#weight'] = 10; $form['actions']['#weight'] = 100; $form['workflow_list_title']['#weight'] = 200; $form['table']['#weight'] = 201; return $form; }