Пример #1
0
 /**
  * {@inheritdoc}
  */
 public function getNextSid($entity, $field_name, $user, $force = FALSE)
 {
     $new_sid = FALSE;
     $current_sid = WorkflowManager::getCurrentStateId($entity, $field_name);
     $current_state = WorkflowState::load($current_sid);
     $options = $current_state->getOptions($entity, $field_name, $user, $force);
     // Loop over every option. To find the next one.
     $flag = $current_state->isCreationState();
     $new_sid = $current_state->id();
     foreach ($options as $sid => $name) {
         if ($flag) {
             $new_sid = $sid;
             break;
         }
         if ($sid == $current_state->id()) {
             $flag = TRUE;
         }
     }
     return $new_sid;
 }
 /**
  * {@inheritdoc}
  *
  * N.B. A large part of this function is taken from CommentDefaultFormatter.
  */
 public function viewElements(FieldItemListInterface $items, $langcode)
 {
     $elements = array();
     $output = array();
     $field_name = $this->fieldDefinition->getName();
     $entity = $items->getEntity();
     $entity_type = $entity->getEntityTypeId();
     $status = $items->status;
     $workflow_settings = $this->getFieldSettings();
     $user = \Drupal::currentUser();
     // @todo #2287057: OK?
     // @todo: Perhaps global user is not always the correct user.
     // E.g., on ScheduledTransition->execute()? But this function is mostly used in UI.
     $current_sid = WorkflowManager::getCurrentStateId($entity, $field_name);
     /* @var $current_state WorkflowState */
     $current_state = WorkflowState::load($current_sid);
     // First compose the current value with the normal formatter from list.module.
     $elements = workflow_state_formatter($entity, $field_name, $current_sid);
     // The state must not be deleted, or corrupted.
     if (!$current_state) {
         return $elements;
     }
     // Check permission, so that even with state change rights,
     // the form can be suppressed from the entity view (#1893724).
     $type_id = $current_state->getWorkflowId();
     if (!\Drupal::currentUser()->hasPermission("access {$type_id} workflow_transition form")) {
         return $elements;
     }
     // Workflows are added to the search results and search index by
     // workflow_node_update_index() instead of by this formatter, so don't
     // return anything if the view mode is search_index or search_result.
     if (in_array($this->viewMode, array('search_result', 'search_index'))) {
         return $elements;
     }
     if ($entity_type == 'comment') {
         // No Workflow form allowed on a comment display.
         // (Also, this avoids a lot of error messages.)
         return $elements;
     }
     // Only build form if user has possible target state(s).
     if (!$current_state->showWidget($entity, $field_name, $user, FALSE)) {
         return $elements;
     }
     // Create a transition, to pass to the form. No need to use setValues().
     $transition = WorkflowTransition::create([$current_sid, 'field_name' => $field_name]);
     $transition->setTargetEntity($entity);
     // Remove the default formatter. We are now building the widget.
     $elements = array();
     // BEGIN Copy from CommentDefaultFormatter
     $elements['#cache']['contexts'][] = 'user.permissions';
     $output['workflows'] = [];
     // Add the WorkflowTransitionForm to the page.
     // $build = $this->viewBuilder->viewMultiple($workflows);
     $build = $this->entityFormBuilder()->getForm($transition, 'add');
     $output['workflows'] += $build;
     // Only show the add workflow form if the user has permission.
     $elements['#cache']['contexts'][] = 'user.roles';
     // Do not show the form for the print view mode.
     $elements[] = $output + array('#workflow_type' => $this->getFieldSetting('workflow_type'), '#workflow_display_mode' => $this->getFieldSetting('default_mode'), 'workflows' => array());
     // END Copy from CommentDefaultFormatter
     return $elements;
 }