From ed31a4071fb56a5f11897dd0846ff7e0f0a6f763 Mon Sep 17 00:00:00 2001 From: Akanksha Singh Date: Tue, 2 May 2023 17:53:09 +0530 Subject: [PATCH 1/4] CTDA9-298: Add Islandora core feature dependency --- islandora_hocr.install | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/islandora_hocr.install b/islandora_hocr.install index 803d5a9..f09f67d 100644 --- a/islandora_hocr.install +++ b/islandora_hocr.install @@ -6,6 +6,7 @@ */ use Drupal\taxonomy\Entity\Term; +use Drupal\field\Entity\FieldStorageConfig; /** * Implements hook_install(). @@ -15,6 +16,12 @@ use Drupal\taxonomy\Entity\Term; * @throws \Drupal\Core\Entity\EntityStorageException */ function islandora_hocr_install() { + $field_storage = Drupal\field\Entity\FieldStorageConfig::loadByName('taxonomy_term', 'field_external_uri'); + if (empty($field_storage) || !in_array('islandora_media_use', $field_storage->getBundles())) { + // Re-import the default config for islandora core feature to make sure fields exist. + \Drupal::service('config.installer')->installDefaultConfig('module', 'islandora_core_feature'); + } + // Generate hOCR media use term. Term::create([ 'parent' => [], @@ -23,4 +30,5 @@ function islandora_hocr_install() { 'description' => 'hOCR Derivative Term', 'field_external_uri' => ['uri' => 'https://discoverygarden.ca/use#hocr'], ])->save(); + } From 91a8ac92645c7179771e6306e38ade876f8e0935 Mon Sep 17 00:00:00 2001 From: Akanksha Singh Date: Tue, 2 May 2023 17:54:31 +0530 Subject: [PATCH 2/4] CTDA9-298: Add Islandora core feature dependency --- islandora_hocr.install | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/islandora_hocr.install b/islandora_hocr.install index f09f67d..e7d4771 100644 --- a/islandora_hocr.install +++ b/islandora_hocr.install @@ -5,8 +5,8 @@ * Install file. */ -use Drupal\taxonomy\Entity\Term; use Drupal\field\Entity\FieldStorageConfig; +use Drupal\taxonomy\Entity\Term; /** * Implements hook_install(). @@ -16,9 +16,10 @@ use Drupal\field\Entity\FieldStorageConfig; * @throws \Drupal\Core\Entity\EntityStorageException */ function islandora_hocr_install() { - $field_storage = Drupal\field\Entity\FieldStorageConfig::loadByName('taxonomy_term', 'field_external_uri'); + $field_storage = FieldStorageConfig::loadByName('taxonomy_term', 'field_external_uri'); if (empty($field_storage) || !in_array('islandora_media_use', $field_storage->getBundles())) { - // Re-import the default config for islandora core feature to make sure fields exist. + // Re-import the default config for islandora + // core feature to make sure fields exist. \Drupal::service('config.installer')->installDefaultConfig('module', 'islandora_core_feature'); } From b855faa4095da7d88981284a67873ebb7b60625a Mon Sep 17 00:00:00 2001 From: Akanksha Singh Date: Thu, 4 May 2023 17:59:57 +0530 Subject: [PATCH 3/4] CTDA9-298: Fix vocabulary creation --- islandora_hocr.info.yml | 1 + islandora_hocr.install | 16 ++++++----- islandora_hocr.module | 60 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 islandora_hocr.module diff --git a/islandora_hocr.info.yml b/islandora_hocr.info.yml index 3a27c05..24f9dc5 100644 --- a/islandora_hocr.info.yml +++ b/islandora_hocr.info.yml @@ -6,3 +6,4 @@ core_version_requirement: ^8 || ^9 dependencies: - islandora:islandora - islandora_core_feature:islandora_core_feature + - drupal:taxonomy diff --git a/islandora_hocr.install b/islandora_hocr.install index e7d4771..82765df 100644 --- a/islandora_hocr.install +++ b/islandora_hocr.install @@ -5,7 +5,6 @@ * Install file. */ -use Drupal\field\Entity\FieldStorageConfig; use Drupal\taxonomy\Entity\Term; /** @@ -16,12 +15,15 @@ use Drupal\taxonomy\Entity\Term; * @throws \Drupal\Core\Entity\EntityStorageException */ function islandora_hocr_install() { - $field_storage = FieldStorageConfig::loadByName('taxonomy_term', 'field_external_uri'); - if (empty($field_storage) || !in_array('islandora_media_use', $field_storage->getBundles())) { - // Re-import the default config for islandora - // core feature to make sure fields exist. - \Drupal::service('config.installer')->installDefaultConfig('module', 'islandora_core_feature'); - } + $entitytypes_fields = [ + 'taxonomy_term' => [ + 'islandora_media_use' => [ + 'field_external_uri', + ], + ], + ]; + + _ensure_fields_exist($entitytypes_fields); // Generate hOCR media use term. Term::create([ diff --git a/islandora_hocr.module b/islandora_hocr.module new file mode 100644 index 0000000..094bc9d --- /dev/null +++ b/islandora_hocr.module @@ -0,0 +1,60 @@ + [ + * 'islandora_media_use' => [ + * 'field_foo', + * 'field_bar', + * ], + * ], + * ]. + * + * @throws \Drupal\Core\Entity\EntityStorageException + */ + +use Drupal\Core\Site\Settings; +use Drupal\field\Entity\FieldConfig; +use Drupal\field\Entity\FieldStorageConfig; +use Drupal\Core\Config\FileStorage; + +/** + * + * @throws \Drupal\Core\Entity\EntityStorageException + */ +function _ensure_fields_exist(array $entitytypes_fields) { + $config_directory = new FileStorage(Settings::get('config_sync_directory')); + foreach ($entitytypes_fields as $entitytype => $bundles) { + foreach ($bundles as $bundle => $fields) { + $bundle_name = 'taxonomy.vocabulary.' . $bundle; + $config_record = $config_directory->read($bundle_name); + $config_storage = \Drupal::service('config.storage'); + $config_storage->write($bundle_name, $config_record); + foreach ($fields as $field) { + $field_storage_name = 'field.storage.' . $entitytype . '.' . $field; + $config_record = $config_directory->read($field_storage_name); + if (!FieldStorageConfig::loadByName($config_record['entity_type'], $config_record['field_name'])) { + FieldStorageConfig::create($config_record)->save(); + } + $field_config_name = 'field.field.' . $entitytype . '.' . $bundle . '.' . $field; + $config_record = $config_directory->read($field_config_name); + if (!FieldConfig::loadByName($config_record['entity_type'], $config_record['bundle'], $config_record['field_name'])) { + FieldConfig::create($config_record)->save(); + } + } + } + } +} From ca7f1718dbc7dbff55753ae8ebd1321ab41ff1e0 Mon Sep 17 00:00:00 2001 From: Akanksha Singh Date: Thu, 4 May 2023 18:04:08 +0530 Subject: [PATCH 4/4] CTDA9-298: Fix codesniffer things --- islandora_hocr.install | 2 +- islandora_hocr.module | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/islandora_hocr.install b/islandora_hocr.install index 82765df..86cda8b 100644 --- a/islandora_hocr.install +++ b/islandora_hocr.install @@ -23,7 +23,7 @@ function islandora_hocr_install() { ], ]; - _ensure_fields_exist($entitytypes_fields); + islandora_hocr_ensure_fields_exist($entitytypes_fields); // Generate hOCR media use term. Term::create([ diff --git a/islandora_hocr.module b/islandora_hocr.module index 094bc9d..febd2fe 100644 --- a/islandora_hocr.module +++ b/islandora_hocr.module @@ -2,6 +2,7 @@ /** * @file + * Module file. */ /** @@ -32,10 +33,11 @@ use Drupal\field\Entity\FieldStorageConfig; use Drupal\Core\Config\FileStorage; /** + * Make sure that fields and config exists. * * @throws \Drupal\Core\Entity\EntityStorageException */ -function _ensure_fields_exist(array $entitytypes_fields) { +function islandora_hocr_ensure_fields_exist(array $entitytypes_fields) { $config_directory = new FileStorage(Settings::get('config_sync_directory')); foreach ($entitytypes_fields as $entitytype => $bundles) { foreach ($bundles as $bundle => $fields) {