You are here

public function QuickNodeCloneEntityFormBuilder::getForm in Quick Node Clone 8

Gets the built and processed entity form for the given entity.

The form may also be retrieved from the cache if the form was built in a previous page load. The form is then passed on for processing, validation, and submission if there is proper input.

$form_state_additions['langcode'] = $langcode;
$form = \Drupal::service('entity.form_builder')
  ->getForm($entity, 'default', $form_state_additions);

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity to be created or edited.

string $operation: (optional) The operation identifying the form variation to be returned. Defaults to 'default'. This is typically used in routing:

_entity_form:
node . book_outline;

where "book_outline" is the value of $operation. The class name for the form for each operation (edit, delete, etc.) can be found in the form section of the handlers entity annotation. For example:


  handlers = {
    "form" = {
      "delete" = "Drupal\node\Form\NodeDeleteForm",
  

Alternatively, the form class can be set from hook_entity_type_build().

array $form_state_additions: (optional) An associative array used to build the current state of the form. Use this to pass additional information to the form, such as the langcode. Defaults to an empty array.

Return value

array The processed form for the given entity and operation.

Overrides EntityFormBuilder::getForm

See also

\Drupal\Core\Form\FormBuilderInterface::getForm()

\Drupal\Core\Entity\EntityTypeInterface::getFormClass()

\Drupal\Core\Entity\EntityTypeInterface::setFormClass()

system_entity_type_build()

File

src/Entity/QuickNodeCloneEntityFormBuilder.php, line 125

Class

QuickNodeCloneEntityFormBuilder
Builds entity forms.

Namespace

Drupal\quick_node_clone\Entity

Code

public function getForm(EntityInterface $original_entity, $operation = 'default', array $form_state_additions = []) {

  // Clone the node using the awesome createDuplicate() core function.

  /** @var \Drupal\node\Entity\Node $new_node */
  $new_node = $original_entity
    ->createDuplicate();
  $new_node
    ->set('uid', $this->currentUser
    ->id());
  $new_node
    ->set('created', time());
  $new_node
    ->set('changed', time());
  $new_node
    ->set('revision_timestamp', time());

  // Get and store groups of original entity, if any.
  $groups = [];
  if (\Drupal::moduleHandler()
    ->moduleExists('gnode')) {

    /** @var \Drupal\Core\Entity\ContentEntityInterface $original_entity */
    $group_contents = GroupContent::loadByEntity($original_entity);
    foreach ($group_contents as $group_content) {
      $groups[] = $group_content
        ->getGroup();
    }
  }
  $form_state_additions['quick_node_clone_groups_storage'] = $groups;

  // Get default status value of node bundle.
  $default_bundle_status = $this->entityTypeManager
    ->getStorage('node')
    ->create([
    'type' => $new_node
      ->bundle(),
  ])->status->value;

  // Clone all translations of a node.
  foreach ($new_node
    ->getTranslationLanguages() as $langcode => $language) {

    /** @var \Drupal\node\Entity\Node $translated_node */
    $translated_node = $new_node
      ->getTranslation($langcode);
    $translated_node = $this
      ->cloneParagraphs($translated_node);
    $this->moduleHandler
      ->alter('cloned_node', $translated_node, $original_entity);

    // Unset excluded fields.
    $config_name = 'exclude.node.' . $translated_node
      ->getType();
    if ($exclude_fields = $this
      ->getConfigSettings($config_name)) {
      foreach ($exclude_fields as $field) {
        unset($translated_node->{$field});
      }
    }
    $prepend_text = "";
    $title_prepend_config = $this
      ->getConfigSettings('text_to_prepend_to_title');
    if (!empty($title_prepend_config)) {
      $prepend_text = $title_prepend_config . " ";
    }
    $clone_status_config = $this
      ->getConfigSettings('clone_status');
    if (!$clone_status_config) {
      $key = $translated_node
        ->getEntityType()
        ->getKey('published');
      $translated_node
        ->set($key, $default_bundle_status);
    }
    $translated_node
      ->setTitle($this
      ->t('@prepend_text@title', [
      '@prepend_text' => $prepend_text,
      '@title' => $translated_node
        ->getTitle(),
    ], [
      'langcode' => $langcode,
    ]));
  }

  // Get the form object for the entity defined in entity definition.
  $form_object = $this->entityTypeManager
    ->getFormObject($translated_node
    ->getEntityTypeId(), $operation);

  // Assign the form's entity to our duplicate!
  $form_object
    ->setEntity($translated_node);
  $form_state = (new FormState())
    ->setFormState($form_state_additions);
  $new_form = $this->formBuilder
    ->buildForm($form_object, $form_state);

  // If we are cloning addresses, we need to reset our delta counter
  // once the form is built.
  $tempstore = $this->privateTempStoreFactory
    ->get('quick_node_clone');
  if ($tempstore
    ->get('address_initial_value_delta') !== NULL) {
    $tempstore
      ->set('address_initial_value_delta', NULL);
  }
  return $new_form;
}