Skip to content
Snippets Groups Projects

Add Generic entity field

3 files
+ 92
0
Compare changes
  • Side-by-side
  • Inline
Files
3
<?php
namespace Drupal\ui_patterns\Plugin\Derivative;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Plugin\Context\ContextDefinition;
/**
* Provides Plugin for every field property of type ui_patterns_source.
*/
class CustomEntityFieldSourceDeriver extends EntityFieldSourceDeriverBase {
/**
* {@inheritdoc}
*/
protected function getDerivativeDefinitionsForEntityStorageField(string $entity_type_id, string $field_name, array $base_plugin_derivative): void {
$id = implode(PluginBase::DERIVATIVE_SEPARATOR, [
$entity_type_id,
$field_name,
]);
$field_type = $this->entityFieldsMetadata[$entity_type_id]["field_storages"][$field_name]["metadata"]["type"];
$target_field_types = $base_plugin_derivative['metadata']['parent_plugin_definition']['metadata']['field_type'] ?? [];
if (in_array($field_type, $target_field_types)) {
$this->derivatives[$id] = array_merge(
$base_plugin_derivative,
[
"label" => $base_plugin_derivative['metadata']['parent_plugin_definition']['label'],
"id" => $id,
"tags" => array_merge($base_plugin_derivative["tags"], $target_field_types),
]);
$field_storage_data = $this->entityFieldsMetadata[$entity_type_id]["field_storages"][$field_name];
$bundle_context_for_properties = (new ContextDefinition('string'))
->setRequired()
->setLabel("Bundle")
->addConstraint('AllowedValues', array_merge($field_storage_data["bundles"] ?? [], [""]));
$this->derivatives[$id]["context_definitions"]["bundle"] = $bundle_context_for_properties;
}
}
}
\ No newline at end of file
Loading