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

[SDPAP-7925]add config files #34

Merged
merged 18 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.'
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This helper function we can keep, cause this can be used for creating any other vocabulary.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good example of a code reuse pattern, good one @yeniatencio

$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', $tidePublicationOperation::'publication', $tidePublicationOperation::'field_publication_authors');
if ($field->get('field_type') === $tidePublicationOperation::'entity_reference') {
$new_field = $field->toArray();
$new_field['field_type'] = $tidePublicationOperation::'entity_reference';
$new_field['description'] = $tidePublicationOperation::'Start typing to choose one publication author.';
$new_field['dependencies'] = [
'config' => [
'field.storage.' . $config->get('id'),
'node.type.' . $tidePublicationOperation::'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();
}
}