function WorkspacesHtmlEntityFormController::getContentResult

Same name in other branches
  1. 10 core/modules/workspaces/src/Controller/WorkspacesHtmlEntityFormController.php \Drupal\workspaces\Controller\WorkspacesHtmlEntityFormController::getContentResult()

Overrides FormController::getContentResult

File

core/modules/workspaces/src/Controller/WorkspacesHtmlEntityFormController.php, line 36

Class

WorkspacesHtmlEntityFormController
Overrides the entity form controller service for workspaces operations.

Namespace

Drupal\workspaces\Controller

Code

public function getContentResult(Request $request, RouteMatchInterface $route_match) : array {
    $form_arg = $this->getFormArgument($route_match);
    $form_object = $this->getFormObject($route_match, $form_arg);
    
    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    $entity = $form_object->getEntity();
    if ($this->workspaceInfo
        ->isEntitySupported($entity)) {
        $active_workspace = $this->workspaceManager
            ->getActiveWorkspace();
        // Prepare a minimal render array in case we need to return it.
        $build['#cache']['contexts'] = $entity->getCacheContexts();
        $build['#cache']['tags'] = $entity->getCacheTags();
        $build['#cache']['max-age'] = $entity->getCacheMaxAge();
        // Prevent entities from being edited if they're tracked in workspace.
        if ($form_object->getOperation() !== 'delete') {
            $constraints = array_values(array_filter($entity->getTypedData()
                ->getConstraints(), function ($constraint) {
                return $constraint instanceof EntityWorkspaceConflictConstraint;
            }));
            if (!empty($constraints)) {
                $violations = $this->typedDataManager
                    ->getValidator()
                    ->validate($entity->getTypedData(), $constraints[0]);
                if (count($violations)) {
                    $build['#markup'] = $violations->get(0)
                        ->getMessage();
                    return $build;
                }
            }
        }
        // Prevent entities from being deleted in a workspace if they have a
        // published default revision.
        if ($form_object->getOperation() === 'delete' && $active_workspace && !$this->workspaceInfo
            ->isEntityDeletable($entity, $active_workspace)) {
            $build['#markup'] = $this->t('This @entity_type_label can only be deleted in the Live workspace.', [
                '@entity_type_label' => $entity->getEntityType()
                    ->getSingularLabel(),
            ]);
            return $build;
        }
    }
    return $this->entityFormController
        ->getContentResult($request, $route_match);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.