Commit 43827f8b authored by Kingdutch's avatar Kingdutch

Remove deprecated unused code

This commit removes some code that was used in earlier versions
that attempted to perform the new entity form submission. This is
now tackled by Form Handlers and solved in a better way.
parent c9419570
......@@ -52,82 +52,6 @@ class EntityAnalyser {
$this->router = $router;
}
/**
* Construct an entity from a virtual form submission.
*
* Takes the action and method of a form with a form_data array to simulate
* the form submission and returns the entity constructed from the form data.
*
* @param string $action
* The path to which the form would normally be submitted.
* @param string $method
* The method by which to submit the form (POST or GET).
* @param array $form_data
* An array containing the form fields with their values.
*
* @return \Drupal\Core\Entity\Entity
* The constructed entity.
*/
public function entityFromFormSubmission($action, $method, array $form_data) {
$form_request = Request::create($action, $method, $form_data);
// Push our form request on the stack so it's used by the form builder.
\Drupal::requestStack()->push($form_request);
try {
$match = $this->router->matchRequest($form_request);
}
catch (\Exception $e) {
throw new NotFoundHttpException('Could not find the entity route.');
}
$operation = NULL;
// Find the first loaded entity.
foreach ($match as $val) {
// Existing entities will provide is with an entity of ContentEntityBase.
if ($val instanceof ContentEntityBase || is_subclass_of($val, "ContentEntityBase")) {
$temp_entity = $val;
$operation = explode('.', $match['_entity_form'])[1];
break;
}
// Entity creation pages provide us a parent ConfigEntityBase inheritor to
// help us determine the entity type and bundle.
elseif ($val instanceof ConfigEntityBase || is_subclass_of($val, "ConfigEntityBase")) {
$config_entity_type = $val->getEntityType();
$type = $config_entity_type->getBundleOf();
$operation = explode('.', $match['_route'])[1];
$storage = $this->entityTypeManager->getStorage($type);
// Set the bundle name where needed.
$type_key = $storage->getEntityType()->get('entity_keys')['bundle'];
// Create a temporary entity so we can load the form.
$temp_entity = $storage->create([$type_key => $val->id()]);
break;
}
}
if (empty($temp_entity)) {
throw new BadRequestHttpException('Could not construct entity from form data');
}
$entity = $this->getUpdatedEntity($temp_entity, $operation);
// Pop our form request from the stack as we're done with it.
\Drupal::requestStack()->pop();
if (empty($entity)) {
throw new NotFoundHttpException("Could not find the edited entity");
}
return $entity;
}
/**
* Takes an entity, renders it and adds the metatag values.
*
......@@ -209,59 +133,6 @@ class EntityAnalyser {
return $this->renderer->renderRoot($render_array);
}
/**
* Create an up to date entity from submitted form data for any entity type.
*
* Uses a temporary entity and a desired operation to retrieve form values
* from the request currently on top of the requestStack and returns an entity
* with those updated values.
*
* @param \Drupal\Core\Entity\EntityInterface $temp_entity
* A temporary entity that is used to gather information
* like entity type and bundle.
* @param string|null $operation
* The operation that the submitted form performs on the entity. Required
* to select the correct form display mode and map submitted fields to those
* available in the form.
*
* @return \Drupal\Core\Entity\Entity
* An entity that contains the values from the submitted form.
*/
protected function getUpdatedEntity(EntityInterface $temp_entity, $operation = NULL) {
$form_handlers = $temp_entity->getEntityType()->get('handlers')['form'];
if (empty($operation) || !isset($form_handlers[$operation])) {
$operation = 'default';
}
$form_state = new FormState();
$form_object = $this->entityTypeManager->getFormObject($temp_entity->getEntityTypeId(), $operation);
$form_object->setEntity($temp_entity);
/** @var \Drupal\Core\Form\FormBuilder $form_builder */
$form_builder = \Drupal::service('form_builder');
$form = $form_builder->buildForm($form_object, $form_state);
if (!empty($form_state->getErrors())) {
// TODO: Handle errors!
sleep(1);
}
// Build our entity from the form state.
/** @var \Drupal\Core\Entity\Entity $entity */
$entity = $form_object->buildEntity($form, $form_state);
// Support ownable entities that might not yet have an owner.
if ($entity instanceof EntityOwnerInterface && empty($entity->getOwner())) {
$owner = User::load(\Drupal::currentUser()->id());
$entity->setOwner($owner);
}
return $entity;
}
/**
* Replace context aware tokens in a metatags array.
*
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment