Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
novicell/custom_forms / src / CustomFormViewBuilder.php
Size: Mime:
<?php

namespace Drupal\custom_forms;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityViewBuilder;

/**
 * Class CustomFormViewBuilder
 *
 * Handles building the view of the custom form.
 *
 * @package Drupal\custom_forms
 *
 */
class CustomFormViewBuilder extends EntityViewBuilder {

  /**
   * {@inheritdoc}
   */
  public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) {
    /** @var \Drupal\custom_forms\CustomFormInterface $entity */
    $route      = \Drupal::routeMatch()->getRouteObject();
    $route_name = \Drupal::routeMatch()->getRouteName();
    $is_admin   = \Drupal::service('router.admin_context')->isAdminRoute($route);
    $build      = ['#markup' => $entity->label()];

    if ($route !== NULL && $route->hasOption('_layout_builder') && $route->getOption('_layout_builder')) {
      return $build;
    }

    $allowed_routes = [
      'entity.custom_form.canonical',
      'entity.custom_form.revision'
    ];

    // We only want to render the form if we're on the canonical route or not in
    // the admin theme.
    // This is done so that we can still preview the form in the backend, but
    // not embed it within a paragraph when editing a node, as that would cause
    // the node form to fail.
    if (in_array($route_name, $allowed_routes) || !$is_admin) {
      $build = \Drupal::formBuilder()->getForm('Drupal\custom_forms\Form\CustomForm', ['custom_form' => $entity]);
    }

    // Return the build, which should either be the label or the form itself.
    return $build;
  }

}