Skip to content
Snippets Groups Projects

Draft: Resolve #3503038 Refactor for SDC component metadata instead of plugin instances

3 files
+ 38
15
Compare changes
  • Side-by-side
  • Inline
Files
3
@@ -104,6 +104,15 @@ abstract class GeneratedFieldExplicitInputUxComponentSourceBase extends Componen
*/
abstract protected function getSdcPlugin(): SdcPlugin;
/**
* The SDC metadata that everything else in this trait builds upon.
*/
abstract protected function getComponentMetadata(): ComponentMetadata;
private function getPropShapes(): array {
return PropShape::getComponentPropsForMetadata($this->configuration['plugin_id'], $this->getComponentMetadata());
}
/**
* {@inheritdoc}
*/
@@ -138,7 +147,7 @@ abstract class GeneratedFieldExplicitInputUxComponentSourceBase extends Componen
private function getDefaultStaticPropSource(string $prop_name): StaticPropSource {
assert(isset($this->configuration['prop_field_definitions']));
assert(is_array($this->configuration['prop_field_definitions']));
$component_schema = $this->getSdcPlugin()->metadata->schema ?? [];
$component_schema = $this->getComponentMetadata()->schema;
if (!array_key_exists($prop_name, $component_schema['properties'] ?? [])) {
throw new \OutOfRangeException(sprintf("'%s' is not a prop on the component '%s'.", $prop_name, $this->getComponentDescription()));
}
@@ -162,7 +171,7 @@ abstract class GeneratedFieldExplicitInputUxComponentSourceBase extends Componen
}
public function getSlotDefinitions(): array {
return $this->getSdcPlugin()->metadata->slots;
return $this->getComponentMetadata()->slots;
}
/**
@@ -247,7 +256,7 @@ abstract class GeneratedFieldExplicitInputUxComponentSourceBase extends Componen
* {@inheritdoc}
*/
public function requiresExplicitInput(): bool {
return !empty($this->getSdcPlugin()->metadata->schema['properties']);
return !empty($this->getComponentMetadata()->schema['properties']);
}
/**
@@ -377,8 +386,7 @@ abstract class GeneratedFieldExplicitInputUxComponentSourceBase extends Componen
): array {
$transforms = [];
assert($entity instanceof FieldableEntityInterface);
$component_plugin = $this->getSdcPlugin();
$component_schema = $component_plugin->metadata->schema ?? [];
$component_schema = $this->getComponentMetadata()->schema ?? [];
// Allow form alterations specific to XB component inputs forms (currently
// only "static prop sources").
@@ -390,7 +398,7 @@ abstract class GeneratedFieldExplicitInputUxComponentSourceBase extends Componen
// To ensure the order of the fields always matches the order of the schema
// we loop over the properties from the schema, but first we have to
// exclude props that aren't storable.
foreach (PropShape::getComponentProps($component_plugin) as $component_prop_expression => $prop_shape) {
foreach ($this->getPropShapes() as $component_prop_expression => $prop_shape) {
$storable_prop_shape = $prop_shape->getStorage();
// @todo Remove this once every SDC prop shape can be stored. See PropShapeRepositoryTest::getExpectedUnstorablePropShapes()
// @todo Create a status report that lists which SDC prop shapes are not storable.
@@ -434,7 +442,7 @@ abstract class GeneratedFieldExplicitInputUxComponentSourceBase extends Componen
throw new \LogicException(sprintf(
"Experience Builder determined the `%s` field widget plugin must be used to populate the `%s` prop on the `%s` component. However, no `xb.transforms` metadata is defined on the field widget plugin definition. This makes it impossible for this widget to work. Please define the missing metadata. See %s for guidance.",
$field_widget_plugin_id,
$component_plugin->getPluginId(),
$component_prop->componentName,
$sdc_prop_name,
'https://git.drupalcode.org/project/experience_builder/-/raw/0.x/experience_builder.api.php?ref_type=heads',
));
@@ -462,11 +470,10 @@ abstract class GeneratedFieldExplicitInputUxComponentSourceBase extends Componen
* {@inheritdoc}
*/
public function getClientSideInfo(ComponentEntity $component): array {
$component_plugin = $this->getSdcPlugin();
assert($component_plugin instanceof SdcPlugin);
$component_metadata = $this->getComponentMetadata();
// @todo Refactor away this instanceof check in https://www.drupal.org/i/3503038
$suggestions = $this instanceof SingleDirectoryComponent
? $this->fieldForComponentSuggester->suggest($component_plugin->getPluginId(), EntityDataDefinition::create('node', 'article'))
? $this->fieldForComponentSuggester->suggest($this->configuration['plugin_id'], EntityDataDefinition::create('node', 'article'))
: [];
$prop_field_definitions = $component->getSettings()['prop_field_definitions'];
@@ -475,7 +482,7 @@ abstract class GeneratedFieldExplicitInputUxComponentSourceBase extends Componen
$unpopulated_props_for_default_markup = [];
$dynamic_prop_source_candidates = [];
$transforms = [];
foreach (PropShape::getComponentProps($component_plugin) as $component_prop_expression => $prop_shape) {
foreach ($this->getPropShapes() as $component_prop_expression => $prop_shape) {
$storable_prop_shape = $prop_shape->getStorage();
// @todo Remove this once every SDC prop shape can be stored. See PropShapeRepositoryTest::getExpectedUnstorablePropShapes()
// @todo Create a status report that lists which SDC prop shapes are not storable.
@@ -545,8 +552,8 @@ abstract class GeneratedFieldExplicitInputUxComponentSourceBase extends Componen
elseif (self::exampleValueRequiresEntity($storable_prop_shape)) {
// An example may be present in the SDC metadata, it just cannot be
// mapped to a default value in the prop source.
if (isset($component_plugin->metadata->schema['properties'][$prop_name]['examples'][0])) {
$default_resolved_value = $component_plugin->metadata->schema['properties'][$prop_name]['examples'][0];
if (isset($component_metadata->schema['properties'][$prop_name]['examples'][0])) {
$default_resolved_value = $component_metadata->schema['properties'][$prop_name]['examples'][0];
}
}
@@ -567,7 +574,7 @@ abstract class GeneratedFieldExplicitInputUxComponentSourceBase extends Componen
// generate a form.
// @see \Drupal\experience_builder\Form\ComponentInputsForm
$field_data[$prop_name] = [
'required' => in_array($prop_name, $component_plugin->metadata->schema['required'] ?? [], TRUE),
'required' => in_array($prop_name, $component_metadata->schema['required'] ?? [], TRUE),
'jsonSchema' => $prop_shape->resolvedSchema,
] + \array_diff_key($default_static_prop_source->toArray(), \array_flip(['value']));
if ($default_resolved_value !== NULL) {
Loading