Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ongoing work] Adds a configuration UI #6

Open
wants to merge 3 commits into
base: 8.x-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions Tests/src/Kernel/Entity/AutovalueSettingsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/**
* @file
* Contains \Drupal\Tests\autovalue\Kernel\AutovalueSettingsTest.
*/

namespace Drupal\Tests\autovalue\Kernel;

use Drupal\autovalue\Entity\AutovalueSettings;
use Drupal\KernelTests\KernelTestBase;

/**
* @coversDefaultClass \Drupal\autovalue\Entity\AutovalueSettings
* @group autovalue
*/
class AutovalueSettingsTest extends KernelTestBase {

/**
* {@inheritdoc}
*/
public static $modules = ['autovalue'];

/**
* @covers ::loadByEntityTypeAndBundle
*/
public function testLoadByEntityTypeAndBundle() {
$setting = AutovalueSettings::loadByEntityTypeAndBundle('example', 'bundle');
$this->assertInstanceOf(AutovalueSettings::class, $setting);
$this->assertEquals('example.bundle', $setting->id());
$this->assertEquals('example', $setting->getTargetEntityTypeId());
$this->assertEquals('bundle', $setting->getTargetBundle());

$setting->save();
}

public function testConfiguration() {
$setting = AutovalueSettings::loadByEntityTypeAndBundle('example', 'bundle');

$setting->getConfiguration()->addInstanceId('my_field', [
'id' => 'token',
'pattern' => 'hello-world'
]);
$setting->save();

$result = $setting->toArray();
unset($result['uuid']);
$this->assertEquals([
'id' => 'example.bundle',
'langcode' => 'en',
'status' => TRUE,
'dependencies' => [],
'target_entity_type_id' => 'example',
'target_bundle' => 'bundle',
'configuration' => [
'my_field' => [
'id' => 'token',
'pattern' => 'hello-world',
],
],
], $result);
}

}
7 changes: 7 additions & 0 deletions autovalue.routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
autovalue.settings_form:
path: /admin/config/content/autovalue
defaults:
_title: Autovalue configuration
_form: \Drupal\autovalue\Form\AutovalueSettingsForm
requirements:
_permission: administer site configuration
47 changes: 47 additions & 0 deletions config/schema/autovalue.schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
autovalue.autovalue_settings.*.*:
type: config_entity
label: Autovalue Settings'
mapping:
id:
type: string
label: 'ID'
target_entity_type_id:
type: string
label: 'Entity Type ID'
target_bundle:
type: string
label: 'Bundle'
configuration:
type: sequence
sequence:
type: autovalue.autovalue.[id]

autovalue.autovalue.*:
type: mapping
mapping:
id:
type: string
label: Plugin ID

autovalue.autovalue.token:
type: mapping
mapping:
id:
type: string
label: Plugin ID
pattern:
type: string
label: Pattern

autovalue.autovalue.random_value:
type: mapping
mapping:
id:
type: string
label: Plugin ID
value_type:
type: string
label: Value type
length:
type: integer
label: Length
94 changes: 94 additions & 0 deletions src/Element/AutovalueConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

/**
* @file
* Contains \Drupal\autovalue\Element\AutovalueConfiguration.
*/

namespace Drupal\autovalue\Element;

use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Render\Element\FormElement;

/**
* Provides autovalue element configuration.
*
* @FormElement("autovalue_configuration")
*/
class AutovalueConfiguration extends FormElement {

/**
* {@inheritdoc}
*/
public function getInfo() {
$class = get_class($this);
return [
'#input' => TRUE,
'#tree' => TRUE,
'#process' => [
[$class, 'processAutovalueConfiguration'],
],
];
}

/**
* Process handler for the autovalue_configuration form element.
*/
public static function processAutovalueConfiguration(&$element, FormStateInterface $form_state, &$form) {
/** @var \Drupal\autovalue\Entity\AutovalueSettings $autovalue_settings */
$autovalue_settings = $element['#default_value'];

/** @var \Drupal\autovalue\Service\AutoValuePluginManagerInterface $autovalue_manager */
$autovalue_manager = \Drupal::service('plugin.manager.autovalue');
$options = array_map(function ($item) {
return $item['label'];
}, $autovalue_manager->getDefinitions());

/** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */
$field_manager = \Drupal::service('entity_field.manager');
$target_entity_type_id = $autovalue_settings->getTargetEntityTypeId();
$target_bundle = $autovalue_settings->getTargetBundle();
$fields = $field_manager->getFieldDefinitions($target_entity_type_id, $target_bundle);

$element['#tree'] = TRUE;
foreach ($fields as $field_name => $field_definition) {
$element[$field_name]['#markup'] = $field_definition->getLabel();

$element[$field_name]['id'] = [
'#type' => 'select',
'#title' => t('Autovalue type'),
'#options' => $options,
'#ajax' => [
'callback' => '\Drupal\autovalue\Element\AutovalueConfiguration::updatePluginConfiguration',
'wrapper' => "autovalue-settings-$target_entity_type_id-$target_bundle-$field_name-configuration",
],
];

$plugin_id = isset($form_state->getUserInput()['settings'][$target_entity_type_id][$target_bundle]['autovalue_configuration'][$field_name]['id']) ? $form_state->getUserInput()['settings'][$target_entity_type_id][$target_bundle]['autovalue_configuration'][$field_name]['id'] : 'default';
// @todo configuration
$plugin = $autovalue_manager->createInstance($plugin_id, []);
$element[$field_name]['configuration'] = [
'#prefix' => '<div id="' . "autovalue-settings-$target_entity_type_id-$target_bundle-$field_name-configuration" . '>',
'#suffix' => '</div>',
];
if ($plugin instanceof PluginFormInterface) {
$element[$field_name]['configuration'] += $plugin->buildConfigurationForm($element[$field_name]['configuration'], $form_state);
}
}

return $element;
}

/**
* {@inheritdoc}
*/
public static function updatePluginConfiguration($form, FormStateInterface $form_state) {
$array_parents = $form_state->getTriggeringElement()['#array_parents'];
array_pop($array_parents);
$array_parents[] = 'configuration';
return NestedArray::getValue($form, $array_parents);
}

}
122 changes: 122 additions & 0 deletions src/Entity/AutovalueSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

/**
* @file
* Contains \Drupal\autovalue\Entity\AutovalueSettings.
*/

namespace Drupal\autovalue\Entity;

use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Plugin\DefaultLazyPluginCollection;

/**
* Defines the autovalue settings entity.
*
* @ConfigEntityType(
* id = "autovalue_settings",
* label = @Translation("Autovalue Settings"),
* admin_permission = "administer site configuration",
* config_prefix = "autovalue_settings",
* entity_keys = {
* "id" = "id"
* },
* config_export = {
* "id",
* "target_entity_type_id",
* "target_bundle",
* "configuration",
* }
* )
*/
class AutovalueSettings extends ConfigEntityBase {

/**
* The ID.
*
* @var string
*/
protected $id;

/**
* The targeted entity type ID.
*
* @var string
*/
protected $target_entity_type_id = '';

/**
* The target bundle.
*
* @var string
*/
protected $target_bundle;

/**
* @var \Drupal\Core\Plugin\DefaultLazyPluginCollection
*/
protected $configuration;

/**
* {@inheritdoc}
*/
public function __construct(array $values, $entity_type) {
parent::__construct($values, $entity_type);

$values += [
'configuration' => [],
];

$this->configuration = new DefaultLazyPluginCollection(\Drupal::service('plugin.manager.autovalue'), $values['configuration']);
}

/**
* {@inheritdoc}
*/
public function toArray() {
$data = parent::toArray();

$data['configuration'] = $data['configuration']->getConfiguration();
return $data;
}

/**
* Returns the plugin configuration.
*
* @return \Drupal\Core\Plugin\DefaultLazyPluginCollection
*/
public function getConfiguration() {
return $this->configuration;
}

/**
* Loads the autovalue settings by entity type.
*
* @param string $entity_type_id
* The entity type ID.
*
* @return static
*/
public static function loadByEntityTypeAndBundle($entity_type_id, $bundle) {
$config = \Drupal::entityTypeManager()->getStorage('autovalue_settings')->load($entity_type_id . '.' . $bundle);
if ($config == NULL) {
$config = AutovalueSettings::create(['id' => $entity_type_id . '.' . $bundle, 'target_entity_type_id' => $entity_type_id, 'target_bundle' => $bundle]);
}
return $config;
}

/**
* @return string
*/
public function getTargetEntityTypeId() {
return $this->target_entity_type_id;
}

/**
* @return string
*/
public function getTargetBundle() {
return $this->target_bundle;
}

}
Loading