diff --git a/config/install/field.field.node.publication.field_publication_authors.yml b/config/install/field.field.node.publication.field_publication_authors.yml index a393643..31bf545 100644 --- a/config/install/field.field.node.publication.field_publication_authors.yml +++ b/config/install/field.field.node.publication.field_publication_authors.yml @@ -4,13 +4,13 @@ dependencies: config: - field.storage.node.field_publication_authors - node.type.publication - - taxonomy.vocabulary.department + - taxonomy.vocabulary.organisation id: node.publication.field_publication_authors field_name: field_publication_authors entity_type: node bundle: publication label: 'Publication author' -description: 'Use this field to select an author. Choose one mostly applicable.' +description: 'Start typing to choose one publication author. (Authors are added in the organisation taxonomy by your Site Admins.)' required: false translatable: false default_value: { } @@ -19,5 +19,10 @@ settings: handler: 'default:taxonomy_term' handler_settings: target_bundles: - department: department + organisation: organisation + sort: + field: name + direction: asc + auto_create: false + auto_create_bundle: department field_type: entity_reference diff --git a/config/install/field.storage.node.field_publication_authors.yml b/config/install/field.storage.node.field_publication_authors.yml index c2e73d1..c9263fb 100644 --- a/config/install/field.storage.node.field_publication_authors.yml +++ b/config/install/field.storage.node.field_publication_authors.yml @@ -2,8 +2,12 @@ langcode: en status: true dependencies: module: + - field_permissions - node - taxonomy +third_party_settings: + field_permissions: + permission_type: public id: node.field_publication_authors field_name: field_publication_authors entity_type: node @@ -12,7 +16,7 @@ settings: target_type: taxonomy_term module: core locked: false -cardinality: -1 +cardinality: 1 translatable: true indexes: { } persist_with_no_fields: false diff --git a/config/install/taxonomy.vocabulary.organisation.yml b/config/install/taxonomy.vocabulary.organisation.yml new file mode 100644 index 0000000..c542f52 --- /dev/null +++ b/config/install/taxonomy.vocabulary.organisation.yml @@ -0,0 +1,7 @@ +langcode: en +status: true +dependencies: { } +name: Organisation +vid: organisation +description: 'Currently used for ''Publication author'' non-mandatory field' +weight: 0 diff --git a/src/TidePublicationOperation.php b/src/TidePublicationOperation.php new file mode 100644 index 0000000..95f0ed8 --- /dev/null +++ b/src/TidePublicationOperation.php @@ -0,0 +1,27 @@ + $vocabulary, + 'description' => '', + 'name' => ucfirst($vocabulary), + ]); + $vocabulary->save(); + } + +} diff --git a/tide_publication.install b/tide_publication.install index 13ead54..d775cd1 100644 --- a/tide_publication.install +++ b/tide_publication.install @@ -9,6 +9,8 @@ use Drupal\Core\Entity\Entity\EntityFormDisplay; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; use Drupal\search_api\Item\Field; +use Drupal\taxonomy\Entity\Vocabulary; +use Drupal\tide_publication\TidePublicationOperation; use Drupal\user\Entity\Role; use Drupal\workflows\Entity\Workflow; @@ -1056,3 +1058,46 @@ function tide_publication_update_8015() { $form_display->save(); } } + +/** + * Update field_publication_authors cardinality and vocabulary to organisation. + */ +function tide_publication_update_8016() { + $tidePublicationOperation = new TidePublicationOperation(); + $vocabulary = 'organisation'; + $config_factory = \Drupal::configFactory(); + $config = $config_factory->getEditable('field.storage.node.field_publication_authors'); + $config->set('cardinality', 1); + $config->save(); + + // Creates vocabulary if doesn't exist. + $vocabularies = Vocabulary::loadMultiple(); + if (!isset($vocabularies[$vocabulary])) { + $tidePublicationOperation->createVocabulary($vocabulary); + } + + $field = FieldConfig::loadByName('node', 'publication', 'field_publication_authors'); + if ($field->get('field_type') === 'entity_reference') { + $new_field = $field->toArray(); + $new_field['field_type'] = 'entity_reference'; + $new_field['description'] = 'Start typing to choose one publication author. (Authors are added in the organisation taxonomy by your Site Admins.)'; + $new_field['dependencies'] = [ + 'config' => [ + 'field.storage.' . $config->get('id'), + 'node.type.publication', + 'taxonomy.vocabulary.' . $vocabulary, + ], + ]; + $new_field['settings'] = [ + 'handler_settings' => [ + 'target_bundles' => [ + 'department' => $vocabulary, + ], + ], + ]; + $new_field = FieldConfig::create($new_field); + $new_field->original = $field; + $new_field->enforceIsNew(FALSE); + $new_field->save(); + } +} diff --git a/tide_publication.module b/tide_publication.module index f46464b..38e0df2 100644 --- a/tide_publication.module +++ b/tide_publication.module @@ -356,4 +356,7 @@ function tide_publication_form_alter(&$form, FormStateInterface $form_state, $fo } } } + if (isset($form['field_publication_authors'])) { + $form['field_publication_authors']['widget'][0]['target_id']['#placeholder'] = t('Start typing'); + } }