You are here

class EntityFormViewBuilder in Twig Tweak 3.1.x

Same name and namespace in other branches
  1. 3.x src/View/EntityFormViewBuilder.php \Drupal\twig_tweak\View\EntityFormViewBuilder

Entity form view builder.

Hierarchy

Expanded class hierarchy of EntityFormViewBuilder

1 string reference to 'EntityFormViewBuilder'
twig_tweak.services.yml in ./twig_tweak.services.yml
twig_tweak.services.yml
1 service uses EntityFormViewBuilder
twig_tweak.entity_form_view_builder in ./twig_tweak.services.yml
Drupal\twig_tweak\View\EntityFormViewBuilder

File

src/View/EntityFormViewBuilder.php, line 13

Namespace

Drupal\twig_tweak\View
View source
class EntityFormViewBuilder {

  /**
   * The entity form builder service.
   *
   * @var \Drupal\Core\Entity\EntityFormBuilderInterface
   */
  protected $entityFormBuilder;

  /**
   * Constructs an EntityFormViewBuilder object.
   */
  public function __construct(EntityFormBuilderInterface $entity_form_builder) {
    $this->entityFormBuilder = $entity_form_builder;
  }

  /**
   * Gets the built and processed entity form for the given entity type.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity.
   * @param string $form_mode
   *   (optional) The mode identifying the form variation to be returned.
   * @param bool $check_access
   *   (optional) Indicates that access check is required.
   *
   * @return array
   *   The processed form for the given entity type and form mode.
   */
  public function build(EntityInterface $entity, string $form_mode = 'default', bool $check_access = TRUE) : array {
    $build = [];
    $operation = $entity
      ->isNew() ? 'create' : 'update';
    $access = $check_access ? $entity
      ->access($operation, NULL, TRUE) : AccessResult::allowed();
    if ($access
      ->isAllowed()) {
      $build = $this->entityFormBuilder
        ->getForm($entity, $form_mode);
    }
    CacheableMetadata::createFromRenderArray($build)
      ->merge(CacheableMetadata::createFromObject($entity))
      ->merge(CacheableMetadata::createFromObject($access))
      ->applyTo($build);
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityFormViewBuilder::$entityFormBuilder protected property The entity form builder service.
EntityFormViewBuilder::build public function Gets the built and processed entity form for the given entity type.
EntityFormViewBuilder::__construct public function Constructs an EntityFormViewBuilder object.