Skip to content
Snippets Groups Projects
Commit 3e8a703a authored by Mikael Meulle's avatar Mikael Meulle
Browse files

Merge branch '3519543-bug--sample' into '2.0.x'

Resolve #3519543 "Bug  sample"

See merge request !371
parents 0d3fa144 a3ad5695
No related branches found
No related tags found
No related merge requests found
Pipeline #476221 failed
......@@ -5,7 +5,10 @@ namespace Drupal\ui_patterns\Entity;
use Drupal\Core\Entity\ContentEntityStorageInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItemInterface;
use Drupal\Core\TempStore\SharedTempStoreFactory;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Sample entity generator.
......@@ -44,7 +47,7 @@ class SampleEntityGenerator implements SampleEntityGeneratorInterface {
*/
public function get(string $entity_type_id, string $bundle_id): EntityInterface {
$tempstore = $this->tempStoreFactory->get('ui_patterns.sample_entity');
if ($entity = $tempstore->get("$entity_type_id.$bundle_id")) {
if ($entity = $tempstore->get("ui_patterns.$entity_type_id.$bundle_id")) {
return $entity;
}
......@@ -54,10 +57,39 @@ class SampleEntityGenerator implements SampleEntityGeneratorInterface {
}
$entity = $entity_storage->createWithSampleValues($bundle_id);
$tempstore->set("$entity_type_id.$bundle_id", $entity);
$this->update($entity);
$tempstore->set("ui_patterns.$entity_type_id.$bundle_id", $entity);
return $entity;
}
/**
* {@inheritdoc}
*
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
public function update(FieldableEntityInterface $entity) : void {
if ($entity->id()) {
return;
}
foreach ($entity->getFields() as $field_name => $items) {
if (!is_subclass_of($items->getItemDefinition()->getClass(), EntityReferenceItemInterface::class) ||
!($items->getFieldDefinition()->getFieldStorageDefinition() instanceof FieldStorageConfig)) {
continue;
}
foreach ($items as &$item) {
// @phpstan-ignore-next-line
if (!($item->entity instanceof FieldableEntityInterface)) {
continue;
}
if ($item->entity->id()) {
$item->entity = $this->get($item->entity->getEntityTypeId(), $item->entity->bundle());
}
// @phpstan-ignore-next-line
$this->update($item->entity);
}
}
}
/**
* {@inheritdoc}
*/
......
......@@ -3,6 +3,7 @@
namespace Drupal\ui_patterns\Entity;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
/**
* Generates a sample entity.
......@@ -41,4 +42,11 @@ interface SampleEntityGeneratorInterface {
*/
public function delete(string $entity_type_id, string $bundle_id) :SampleEntityGeneratorInterface;
/**
* Update a sample entity.
*
* This method is used to update a sample entity and remove real entities.
*/
public function update(FieldableEntityInterface $entity) : void;
}
......@@ -217,7 +217,7 @@ abstract class DerivableContextSourceBase extends SourcePluginBase {
"#tree" => TRUE,
];
if (empty($options_derivable_contexts)) {
$form["derivable_context"] = [
$form[""] = [
"#markup" => $this->t("Not available"),
];
return $form;
......
......@@ -104,6 +104,7 @@ class LayoutBuilderContextEntityResolver implements ContextEntityResolverInterfa
*/
protected function guessLayoutBuilderEntityFromContexts(array $section_storage_contexts) : ?EntityInterface {
if (array_key_exists("entity", $section_storage_contexts) && ($entity = $section_storage_contexts["entity"]->getContextValue())) {
$this->sampleEntityGenerator->update($entity);
return $entity;
}
// Search for the entity in the context (no matter the name).
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment