Created
April 13, 2023 03:23
-
-
Save angrytoast/32f12fd9624a36a9a8ee35881e95aae9 to your computer and use it in GitHub Desktop.
Webform patch proposal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/WebformEntityReferenceManager.php b/src/WebformEntityReferenceManager.php | |
index b99eb96e4..5c53f495d 100644 | |
--- a/src/WebformEntityReferenceManager.php | |
+++ b/src/WebformEntityReferenceManager.php | |
@@ -82,6 +82,13 @@ class WebformEntityReferenceManager implements WebformEntityReferenceManagerInte | |
*/ | |
protected $fieldNames = []; | |
+ /** | |
+ * Cache of paragraph field names by entity type and bundle. | |
+ * | |
+ * @var array | |
+ */ | |
+ protected $paragraphFieldNames = []; | |
+ | |
/** | |
* Constructs a WebformEntityReferenceManager object. | |
* | |
@@ -194,14 +201,15 @@ class WebformEntityReferenceManager implements WebformEntityReferenceManagerInte | |
* {@inheritdoc} | |
*/ | |
public function getFieldNames(EntityInterface $entity = NULL) { | |
- if ($entity === NULL || !$entity instanceof FieldableEntityInterface) { | |
+ if (!$entity instanceof FieldableEntityInterface) { | |
return []; | |
} | |
// Cache the source entity's field names. | |
- $entity_id = $entity->getEntityTypeId() . '-' . $entity->id(); | |
- if (isset($this->fieldNames[$entity_id])) { | |
- return $this->fieldNames[$entity_id]; | |
+ $entity_type_id = $entity->getEntityTypeId(); | |
+ $entity_bundle = $entity->bundle(); | |
+ if (isset($this->fieldNames[$entity_type_id][$entity_bundle])) { | |
+ return $this->fieldNames[$entity_type_id][$entity_bundle]; | |
} | |
$field_names = []; | |
@@ -217,7 +225,7 @@ class WebformEntityReferenceManager implements WebformEntityReferenceManagerInte | |
// Sort fields alphabetically. | |
ksort($field_names); | |
- $this->fieldNames[$entity_id] = $field_names; | |
+ $this->fieldNames[$entity_type_id][$entity_bundle] = $field_names; | |
return $field_names; | |
} | |
@@ -242,16 +250,15 @@ class WebformEntityReferenceManager implements WebformEntityReferenceManagerInte | |
public function getWebforms(EntityInterface $entity = NULL) { | |
$cid = 'webform_' . $entity->getEntityTypeId() . '_' . $entity->id() . '_webforms'; | |
- // Cache the source entity's loaded webform ids permanently. | |
+ // Avoid a potentially expensive lookup if we can. | |
+ if (isset($this->webforms[$cid])) { | |
+ return $this->webforms[$cid]; | |
+ } | |
$cached = $this->cache->get($cid); | |
if ($cached) { | |
$this->webforms[$cid] = $cached->data | |
? $this->entityTypeManager->getStorage('webform')->loadMultiple($cached->data) | |
: []; | |
- } | |
- | |
- // Cache the source entity's loaded webforms per request. | |
- if (isset($this->webforms[$cid])) { | |
return $this->webforms[$cid]; | |
} | |
@@ -281,6 +288,8 @@ class WebformEntityReferenceManager implements WebformEntityReferenceManagerInte | |
$webforms[$target_id] = $target_entities[$target_id]; | |
} | |
+ // Cache the source entity's loaded webforms per request and permanently | |
+ // for future requests. | |
$this->webforms[$cid] = $webforms; | |
$this->cache->set($cid, array_keys($webforms), Cache::PERMANENT, $entity->getCacheTags()); | |
@@ -307,7 +316,7 @@ class WebformEntityReferenceManager implements WebformEntityReferenceManagerInte | |
return; | |
} | |
// Make sure the entity exists and is fieldable. | |
- if ($entity === NULL || !$entity instanceof FieldableEntityInterface) { | |
+ if (!$entity instanceof FieldableEntityInterface) { | |
return; | |
} | |
@@ -345,8 +354,21 @@ class WebformEntityReferenceManager implements WebformEntityReferenceManagerInte | |
* An array of paragraph field names. | |
*/ | |
protected function getParagraphFieldNames(EntityInterface $entity) { | |
+ $entity_type_id = $entity->getEntityTypeId(); | |
+ $cid = 'webform_' . $entity->getEntityTypeId() . '_paragraph_field_names'; | |
+ | |
+ // Try to get a cached value and avoid loading storage config. | |
+ if (isset($this->paragraphFieldNames[$entity_type_id])) { | |
+ return $this->paragraphFieldNames[$entity_type_id]; | |
+ } | |
+ $cached = $this->cache->get($cid); | |
+ if ($cached) { | |
+ $this->paragraphFieldNames[$entity_type_id] = $cached->data; | |
+ return $this->paragraphFieldNames[$entity_type_id]; | |
+ } | |
+ | |
$fields = $this->entityTypeManager->getStorage('field_storage_config')->loadByProperties([ | |
- 'entity_type' => $entity->getEntityTypeId(), | |
+ 'entity_type' => $entity_type_id, | |
'type' => 'entity_reference_revisions', | |
]); | |
@@ -357,6 +379,11 @@ class WebformEntityReferenceManager implements WebformEntityReferenceManagerInte | |
$field_names[$field_name] = $field_name; | |
} | |
} | |
+ | |
+ // Cache the paragraph field names for this request and permanently for | |
+ // future requests. | |
+ $this->paragraphFieldNames[$entity_type_id] = $field_names; | |
+ $this->cache->set($cid, $field_names, Cache::PERMANENT); | |
return $field_names; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment