Skip to content

Instantly share code, notes, and snippets.

@chri5tia
Last active September 1, 2022 18:35
Show Gist options
  • Save chri5tia/f26ba664b413fe16c39f0436e901f2e3 to your computer and use it in GitHub Desktop.
Save chri5tia/f26ba664b413fe16c39f0436e901f2e3 to your computer and use it in GitHub Desktop.
tablefield patch update
/**
* Adds to tablefield patch
* https://www.drupal.org/project/tablefield/issues/3128030
* to resolve entity mismatch and db constraint issues when applying existing D8 patch:
* https://www.drupal.org/project/tablefield/issues/3128030#comment-13830672
* Existing patch: https://www.drupal.org/files/issues/2020-09-21/tablefield-header-orientation-3128030-13.patch
**/
/**
* Updates tablefield entity manager storage definition to include
* Move to a patch or helper module
* $dl = definition listener
* $fm = field manager
* $sm =
* function tablefield_update_8003() {
* /** @var \Drupal\Core\Entity\EntityFieldManager $fm */
* $fm = \Drupal::service('entity_field.manager');
* $storage_definition = $fm->getFieldStorageDefinitions('paragraph');
* if (isset($storage_definition['field_table'])) {
* /** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepository $sm */
* $sm = \Drupal::service('entity.last_installed_schema.repository');
* $original_storage_definition = $sm->getLastInstalledFieldStorageDefinitions('paragraph');
* /** @var \Drupal\Core\Field\FieldStorageDefinitionListener $dl */
* $dl = \Drupal::service('field_storage_definition.listener');
* $dl->onFieldStorageDefinitionUpdate($storage_definition['field_table'], $original_storage_definition['field_table']);
* }
* }
**/
/**
* Section of patch that this work applies to
*
* diff --git a/tablefield.install b/tablefield.install
* index d5ea4fb..8f97d10 100644
* --- a/tablefield.install
* +++ b/tablefield.install
* @@ -17,6 +17,24 @@ function tablefield_update_8001() {
* ]);
* }
* +/**
* + * Add columns for setting row and column header per field value to the database.
* + */
* +function tablefield_update_8002() {
* + tablefield_add_new_column('row_header', [
* + 'type' => 'int',
* + 'size' => 'tiny',
* + 'default' => 0,
* + 'not null' => TRUE,
* + ]);
* + tablefield_add_new_column('column_header', [
* + 'type' => 'int',
* + 'size' => 'tiny',
* + 'default' => 0,
* + 'not null' => TRUE,
* + ]);
* +}
* +
* /**
* * Helper function to add new columns to the field schema.
* *
**/
/**
* Example code from drupal.org for invoking entity_definition_update()
* https://www.drupal.org/project/entity_definition_update
*
* function your_module_update_8001() {
* // First we need to get update manager.
* $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
*
* // Load the storage definition of the field.
* $field_storage = $definition_update_manager->getFieldStorageDefinition('field_name',
* 'node');
* // Set a new list of options for the list field.
* $field_storage->setSettings([
* 'allowed_values' => [
* 'link' => 'Link',
* 'posts' => 'Posts',
* 'events' => 'Events',
* 'page' => 'Page',
* ],
* ]);
*
* // Update the field storage definition.
* $definition_update_manager->updateFieldStorageDefinition($field_storage);
* }
**/
/**
* Updates paragraph tablefield entity definitions to
* Include new row_header and column_header columns
* By invoking entity_definition_update()
* TODO: Ultimately patch this code to tablefield.install file after tablefiled_update_8001()
**/
function tablefield_update_8002() {
// Call on the update manager
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
// Load the storage definition of the field.
// TODO Add paragraph entity in here somewhere/check this next line
// https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21EntityDefinitionUpdateManager.php/function/EntityDefinitionUpdateManager%3A%3AgetFieldStorageDefinition/8.2.x
$field_storage = $definition_update_manager->getFieldStorageDefinition('row_header','column_header');
// Set new options
$field_storage->setSettings([
// TODO: Add options for row_header and column_header: 'type' => 'int', 'size' => 'tiny', 'default' => 0, 'not null' => TRUE
tablefield_add_new_column('row_header', [
'type' => 'int',
'size' => 'tiny',
'default' => 0,
'not null' => TRUE,
]);
tablefield_add_new_column('column_header', [
'type' => 'int',
'size' => 'tiny',
'default' => 0,
'not null' => TRUE,
]);
// Update the field storage definition.
$definition_update_manager->updateFieldStorageDefinition($field_storage);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment