You are here

class NodeCloneFormController in Node clone 8

Wrapping controller for entity forms that serve as the main page body.

This class is very similar to \Drupal\Core\Entity\HtmlEntityFormController

Hierarchy

Expanded class hierarchy of NodeCloneFormController

1 string reference to 'NodeCloneFormController'
node_clone.services.yml in ./node_clone.services.yml
node_clone.services.yml
1 service uses NodeCloneFormController
node_clone.form_controller in ./node_clone.services.yml
Drupal\node_clone\Controller\NodeCloneFormController

File

src/Controller/NodeCloneFormController.php, line 23

Namespace

Drupal\node_clone\Controller
View source
class NodeCloneFormController extends FormController {
  use StringTranslationTrait;

  /**
   * The entity manager service.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
   * @var \Drupal\node\Access\NodeAddAccessCheck
   */
  protected $nodeAddAccessCheck;

  /**
   * Constructs a new \Drupal\Core\Routing\Enhancer\FormEnhancer object.
   *
   * @param \Drupal\Core\Controller\ControllerResolverInterface $resolver
   *   The controller resolver.
   * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
   *   The form builder.
   * @param \Drupal\Core\Entity\EntityManagerInterface $manager
   *   The entity manager.
   * @param \Drupal\node\Access\NodeAddAccessCheck $node_add_access
   *   Node add access checker.
   */
  public function __construct(ControllerResolverInterface $resolver, FormBuilderInterface $form_builder, EntityManagerInterface $manager, NodeAddAccessCheck $node_add_access) {
    parent::__construct($resolver, $form_builder);
    $this->entityManager = $manager;
    $this->nodeAddAccessCheck = $node_add_access;
  }

  /**
   * {@inheritdoc}
   */
  protected function getFormArgument(RouteMatchInterface $route_match) {
    return 'node';
  }

  /**
   * {@inheritdoc}
   *
   * Instead of a class name or service ID, $form_arg will be a string
   * representing the entity and operation being performed.
   * Consider the following route:
   * @code
   *   path: '/foo/{node}/bar'
   *   defaults:
   *     _entity_form: 'node.edit'
   * @endcode
   * This means that the edit form for the node entity will used.
   * If the entity type has a default form, only the name of the
   * entity {param} needs to be passed:
   * @code
   *   path: '/foo/{node}/baz'
   *   defaults:
   *     _entity_form: 'node'
   * @endcode
   */
  protected function getFormObject(RouteMatchInterface $route_match, $form_arg) {

    // If no operation is provided, use 'default'.
    $form_arg .= '.default';
    list($entity_type_id, $operation) = explode('.', $form_arg);
    $form_object = $this->entityManager
      ->getFormObject($entity_type_id, $operation);

    // Allow the entity form to determine the entity object from a given route
    // match.
    $entity = $form_object
      ->getEntityFromRouteMatch($route_match, $entity_type_id);

    // Clone the entity using the awesome createDuplicate() core function
    $new_entity = $entity
      ->createDuplicate();
    $new_entity
      ->setTitle($this
      ->t('Clone of ') . $new_entity
      ->getTitle());
    $form_object
      ->setEntity($new_entity);
    return $form_object;
  }

  /**
   * @param \Drupal\Core\Session\AccountInterface $account
   * @param \Drupal\node\NodeInterface
   * @return bool
   */
  public function access(AccountInterface $account, NodeInterface $node) {
    if ($account
      ->hasPermission('clone node') || $node->uid->value === $account
      ->id() && $account
      ->hasPermission('clone own nodes')) {
      $access = new AccessResultAllowed();
    }
    else {
      $access = new AccessResultForbidden();
    }
    $access
      ->addCacheableDependency($node);
    $access
      ->cachePerPermissions();
    if ($access
      ->isAllowed()) {
      $access = $access
        ->andIf($node
        ->access('view', $account, TRUE));
    }
    if ($access
      ->isAllowed()) {
      $node_type = NodeType::load($node
        ->getType());
      $access = $access
        ->andIf($this->nodeAddAccessCheck
        ->access($account, $node_type));
    }
    return $access;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormController::$argumentResolver protected property The argument resolver.
FormController::$controllerResolver Deprecated protected property The controller resolver.
FormController::$formBuilder protected property The form builder.
FormController::getContentResult public function Invokes the form and returns the result.
NodeCloneFormController::$entityManager protected property The entity manager service.
NodeCloneFormController::$nodeAddAccessCheck protected property
NodeCloneFormController::access public function
NodeCloneFormController::getFormArgument protected function Extracts the form argument string from a request. Overrides FormController::getFormArgument
NodeCloneFormController::getFormObject protected function Instead of a class name or service ID, $form_arg will be a string representing the entity and operation being performed. Consider the following route: Overrides FormController::getFormObject
NodeCloneFormController::__construct public function Constructs a new \Drupal\Core\Routing\Enhancer\FormEnhancer object. Overrides FormController::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.