Skip to content
Snippets Groups Projects
Commit 72b89729 authored by Daniel Cothran's avatar Daniel Cothran
Browse files

Issue #3418998 by andileco: Tables need a primary key

parent 214b1004
No related branches found
No related tags found
1 merge request!9Update DenormalizerTable.php
Pipeline #105206 passed with warnings
......@@ -20,7 +20,7 @@ function denormalizer_cron() {
*/
function denormalizer_entity_update(EntityInterface $entity) {
if ($entity->getEntityType()->getGroup() == 'content') {
DenormalizerTable::onContentEntityCRUD($entity);
DenormalizerTable::onContentEntityCrud($entity);
}
}
......@@ -29,7 +29,7 @@ function denormalizer_entity_update(EntityInterface $entity) {
*/
function denormalizer_entity_insert(EntityInterface $entity) {
if ($entity->getEntityType()->getGroup() == 'content') {
DenormalizerTable::onContentEntityCRUD($entity, TRUE);
DenormalizerTable::onContentEntityCrud($entity, TRUE);
}
}
......@@ -38,6 +38,6 @@ function denormalizer_entity_insert(EntityInterface $entity) {
*/
function denormalizer_entity_delete(EntityInterface $entity) {
if ($entity->getEntityType()->getGroup() == 'content') {
DenormalizerTable::onContentEntityCRUD($entity, FALSE, TRUE);
DenormalizerTable::onContentEntityCrud($entity, FALSE, TRUE);
}
}
......@@ -4,10 +4,9 @@ namespace Drupal\denormalizer;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\denormalizer\Entity\DenormalizerTableInterface;
/**
* Defines the list builder for denormalizer tables config entities.
* Defines the list builder for Denormalizer tables config entities.
*/
class DenormalizerTableListBuilder extends ConfigEntityListBuilder {
......
This diff is collapsed.
......@@ -5,64 +5,136 @@ namespace Drupal\denormalizer\Entity;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityInterface;
/**
* Provides an interface for defining Denormalizer table entities.
*/
interface DenormalizerTableInterface extends ConfigEntityInterface {
/**
* Gets the denormalizer table source ID.
* Gets the Denormalizer table source ID.
*
* @return string
* The denormalizer table source ID.
* The Denormalizer table source ID.
*/
public function getSourceId();
public function getSourceId(): string;
/**
* Sets the denormalizer table source ID.
* Sets the Denormalizer table source ID.
*
* @param string $source_id
* The denormalizer table source ID.
* The Denormalizer table source ID.
*
* @return $this
*/
public function setSourceId($source_id);
/**
* Gets the denormalizer table source configuration.
* Gets the Denormalizer table source configuration.
*
* @return array
* The denormalizer table source configuration.
* The Denormalizer table source configuration.
*/
public function getSourceConfiguration();
public function getSourceConfiguration(): array;
/**
* Sets the denormalizer table source configuration.
* Sets the Denormalizer table source configuration.
*
* @param array $configuration
* The denormalizer table source configuration.
* The Denormalizer table source configuration.
*
* @return $this
*/
public function setSourceConfiguration(array $configuration);
public function getDbTableName();
/**
* Gets the Denormalizer table source entity type.
*
* @return string
* The Denormalizer table source entity type.
*/
public function getDbTableName(): string;
public function getBaseTable();
/**
* Gets the Denormalizer base table name.
*
* @return string
* The Denormalizer base table name.
*/
public function getBaseTable(): string;
public function createDatabaseTable(bool $init = FALSE);
/**
* Creates the Denormalizer database table.
*
* @param bool $init
* The flag to initialize the table.
*
* @return bool
* The Denormalizer table.
*/
public function createDatabaseTable(bool $init = FALSE): bool;
public function insertDataFromEntityIntoDbTable(EntityInterface $entity);
/**
* Inserts data from an entity into the database table.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to insert into the database table.
*
* @return int|string|null
* The inserted row ID.
*/
public function insertDataFromEntityIntoDbTable(EntityInterface $entity): int|string|null;
/**
* Updates data from an entity into the database table.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to update into the database table.
*/
public function updateDataFromEntityIntoDbTable(EntityInterface $entity);
/**
* Deletes data from an entity into the database table.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to delete from the database table.
*/
public function deleteDataFromEntityIntoDbTable(EntityInterface $entity);
public function getDbTableFieldsSchema();
/**
* Gets the schema for the database table.
*/
public function getDbTableFieldsSchema() : array;
public function isWebformSubmission();
/**
* Checks if the source is a webform submission.
*/
public function isWebformSubmission(): bool;
public function getEntitySelectedFields(bool $base_field_only = FALSE);
/**
* Gets the entity selected fields.
*
* @param bool $base_field_only
* The flag to get only the base fields.
*
* @return array
* The entity selected fields.
*/
public function getEntitySelectedFields(bool $base_field_only = FALSE): array;
public function getWebformSubmissionSelectedElements();
/**
* Gets the webform submission selected elements.
*
* @return array
* The webform submission selected elements.
*/
public function getWebformSubmissionSelectedElements(): array;
public function countRows();
/**
* Counts the number of rows.
*
* @return int
* The number of rows in the table.
*/
public function countRows(): int;
}
......@@ -2,12 +2,10 @@
namespace Drupal\denormalizer\Form;
use Drupal\Core\Database\Database;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\denormalizer\Plugin\Denormalizer\SchemaDenormalizerManager;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -15,13 +13,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*/
class DenormalizerSettingsForm extends ConfigFormBase {
/**
* The schema denormalizer plugin manager.
*
* @var \Drupal\denormalizer\Plugin\Denormalizer\SchemaDenormalizerManager
*/
protected $schemaDenormalizerManager;
/**
* The entity bundle service.
*
......@@ -34,14 +25,11 @@ class DenormalizerSettingsForm extends ConfigFormBase {
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* Config factory.
* @param \Drupal\denormalizer\Plugin\Denormalizer\SchemaDenormalizerManager $schemaDenormalizerManager
* The Schema denormalizer manager.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundleInfo
* The bundle info.
*/
public function __construct(ConfigFactoryInterface $configFactory, SchemaDenormalizerManager $schemaDenormalizerManager, EntityTypeBundleInfoInterface $bundleInfo) {
public function __construct(ConfigFactoryInterface $configFactory, EntityTypeBundleInfoInterface $bundleInfo) {
parent::__construct($configFactory);
$this->schemaDenormalizerManager = $schemaDenormalizerManager;
$this->bundleInfo = $bundleInfo;
}
......@@ -51,7 +39,6 @@ class DenormalizerSettingsForm extends ConfigFormBase {
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('plugin.manager.schema_denormalizer'),
$container->get('entity_type.bundle.info')
);
}
......@@ -100,25 +87,6 @@ class DenormalizerSettingsForm extends ConfigFormBase {
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$prefix = Database::getConnection()->tablePrefix();
$no_prefix = $form_state->getValue('denormalizer_db') == 'local' && !$form_state->getValue('denormalizer_view_prefix');
$same_prefix = $form_state->getValue('denormalizer_db') == 'local' && $form_state->getValue('denormalizer_view_prefix') == $prefix;
if ($no_prefix || $same_prefix) {
$form_state->setErrorByName('denormalizer_view_prefix', $this->t('Using the local database requires a view prefix. Otherwise your tables will be overwritten!'));
}
if ($form_state->getValue('denormalizer_db') == 'external' && !$form_state->getValue('denormalizer_db_prefix')) {
$form_state->setErrorByName('denormalizer_db_prefix', $this->t('Database prefix is required if using an external database.'));
}
}
/**
* {@inheritdoc}
*/
......
......@@ -3,14 +3,24 @@
namespace Drupal\denormalizer\Form;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\denormalizer\Denormalizer;
use Drupal\denormalizer\Entity\DenormalizerTableInterface;
use Drupal\webform\WebformInterface;
/**
* Denormalizer table form.
*/
class DenormalizerTableForm extends EntityForm {
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
......@@ -33,7 +43,8 @@ class DenormalizerTableForm extends EntityForm {
}
// The form state will have a plugin value if #ajax was used.
$source = $form_state->getValue('source', $table->getSourceId());
// Pass the plugin configuration only if the plugin hasn't been changed via #ajax.
// Pass the plugin configuration only if the plugin hasn't been changed
// via #ajax.
$source_configuration = $table->getSourceId() == $source ? $table->getSourceConfiguration() : [];
$wrapper_id = Html::getUniqueId('denormalizer-table-form');
......@@ -123,7 +134,7 @@ class DenormalizerTableForm extends EntityForm {
if ($this->entity->getSourceId() !== 'entity') {
$this->messenger()
->addWarning($this->t('Only @entity config entity was created! None entity source are\'t currently supported!'));
->addWarning($this->t('Only @entity config entity was created! None entity source are not currently supported!'));
$form_state->setRedirect('entity.denormalizer_table.collection');
return;
}
......@@ -156,7 +167,18 @@ class DenormalizerTableForm extends EntityForm {
batch_set($batch);
}
private function buildSourceConfigurationForm($sub_form, FormStateInterface $form_state) {
/**
* Build the source confirguration form.
*
* @param array $sub_form
* The sub form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*
* @return array
* The source configuration form.
*/
private function buildSourceConfigurationForm($sub_form, FormStateInterface $form_state): array {
$source_id = $sub_form['#denormalizer_table_source_id'];
$configuration = $sub_form['#denormalizer_table_configuration'];
$wrapper_id = Html::getUniqueId('denormalizer-table-form--configuration');
......@@ -269,16 +291,36 @@ class DenormalizerTableForm extends EntityForm {
return $sub_form;
}
private function getBundleOptions($entity_type) {
/**
* Get the bundle options for a given entity type.
*
* @param string $entity_type
* The entity type.
*
* @return array
* The bundle options.
*/
private function getBundleOptions(string $entity_type): array {
if (!$entity_type) {
return [];
}
return array_map(function($bundle_info) {
return array_map(function ($bundle_info) {
return $bundle_info['label'];
}, \Drupal::service('entity_type.bundle.info')->getBundleInfo($entity_type));
}
private function getEntityBaseFieldsOptions(string $entity_type_id, bool $changed_key = FALSE) {
/**
* Get the base fields options for a given entity type.
*
* @param string $entity_type_id
* The entity type ID.
* @param bool $changed_key
* Whether to include the changed key.
*
* @return array
* The base fields options.
*/
private function getEntityBaseFieldsOptions(string $entity_type_id, bool $changed_key = FALSE): array {
if (!$entity_type_id) {
return [];
}
......@@ -291,7 +333,18 @@ class DenormalizerTableForm extends EntityForm {
return $options;
}
private function getEntityBundleFieldsOptions(string $entity_type_id, string $bundle) {
/**
* Get the bundle fields options for a given entity type and bundle.
*
* @param string $entity_type_id
* The entity type ID.
* @param string $bundle
* The bundle.
*
* @return array
* The bundle fields options.
*/
private function getEntityBundleFieldsOptions(string $entity_type_id, string $bundle): array {
if (!$entity_type_id || !$bundle) {
return [];
}
......@@ -323,14 +376,24 @@ class DenormalizerTableForm extends EntityForm {
* @return \Drupal\Core\Entity\EntityFieldManagerInterface
* The entity field manager.
*/
private function entityFieldManager() {
private function entityFieldManager(): EntityFieldManagerInterface {
if (empty($this->entityFieldManager)) {
$this->entityFieldManager = \Drupal::service('entity_field.manager');
}
return $this->entityFieldManager;
}
private function processEntityKeysOptions(array &$element, $entity_type_id) {
/**
* Process the entity keys options.
*
* @param array $element
* The element.
* @param string $entity_type_id
* The entity type ID.
*
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
private function processEntityKeysOptions(array &$element, string $entity_type_id): void {
$entity_keys = array_flip($this->entityTypeManager
->getDefinition($entity_type_id)
->getKeys());
......@@ -342,7 +405,20 @@ class DenormalizerTableForm extends EntityForm {
}
}
private function getEntityIdsToPopulate(DenormalizerTableInterface $dn_table, $entity_storage, $entity_type_definition) {
/**
* Get the entity IDs to populate.
*
* @param \Drupal\denormalizer\Entity\DenormalizerTableInterface $dn_table
* The denormalizer table.
* @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
* The entity storage.
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type_definition
* The entity type definition.
*
* @return array
* The entity IDs to populate.
*/
private function getEntityIdsToPopulate(DenormalizerTableInterface $dn_table, EntityStorageInterface $entity_storage, EntityTypeInterface $entity_type_definition): array {
$configuration = $dn_table->getSourceConfiguration();
$bundle = $configuration['bundle'];
$bundle_key = $entity_type_definition->getKey('bundle');
......@@ -356,10 +432,24 @@ class DenormalizerTableForm extends EntityForm {
return $query->execute();
}
/**
* Get the webform.
*
* @param string $webform_id
* The webform ID.
*
* @return \Drupal\webform\WebformInterface|null
* The webform.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
private function getWebform(string $webform_id): ?WebformInterface {
/** @var \Drupal\webform\WebformEntityStorageInterface $webform_storage */
$webform_storage = $this->entityTypeManager->getStorage('webform');
return $webform_storage->load($webform_id);
/** @var \Drupal\webform\WebformInterface $webform */
$webform = $webform_storage->load($webform_id);
return $webform;
}
}
......@@ -10,7 +10,7 @@ use Drupal\node\Entity\NodeType;
use Drupal\node\NodeTypeInterface;
/**
* Test the denormalizer table when the source is an entity.
* Test the Denormalizer table when the source is an entity.
*
* @group denormalizer
*/
......@@ -141,7 +141,7 @@ class EntitySourceDenormalizedTableTest extends KernelTestBase {
* Sets up the page node type.
*
* @return \Drupal\node\NodeTypeInterface
* The node type.
* The node type.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment