Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
[SDPAP-7925]add config files (#34)
Browse files Browse the repository at this point in the history
* add config files

* move config files

* Added hook update to change publication author field cardinality and vocabulary

* Fixed lint errors

* Updated field description

* Check if field type is an entity reference

* remove uuid

* moved to install

* fixed lint

* clean up

* fixed lint

* removed const

* fix lint

* fix lint

* added holder text in publication author field

* update help text

* updated help text

* moved if conditional
  • Loading branch information
yeniatencio authored Sep 14, 2023
1 parent a419a42 commit 69e8e8f
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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: { }
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions config/install/taxonomy.vocabulary.organisation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
langcode: en
status: true
dependencies: { }
name: Organisation
vid: organisation
description: 'Currently used for ''Publication author'' non-mandatory field'
weight: 0
27 changes: 27 additions & 0 deletions src/TidePublicationOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Drupal\tide_publication;

use Drupal\taxonomy\Entity\Vocabulary;

/**
* Tide publication modules operations.
*/
class TidePublicationOperation {

/**
* Creates a new vocabulary.
*
* @param string $vocabulary
* The new vocabulary.
*/
public function createVocabulary(string $vocabulary) {
$vocabulary = Vocabulary::create([
'vid' => $vocabulary,
'description' => '',
'name' => ucfirst($vocabulary),
]);
$vocabulary->save();
}

}
45 changes: 45 additions & 0 deletions tide_publication.install
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}
}
3 changes: 3 additions & 0 deletions tide_publication.module
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

0 comments on commit 69e8e8f

Please sign in to comment.