Skip to content
Snippets Groups Projects
Commit 556d293c authored by Jacob Rockowitz's avatar Jacob Rockowitz
Browse files

Merge branch '3520786-refactor-default-taxonomy' into '1.0.x'

Issue #3520786: Refactor schemadotorg_taxonomy.module's default vocabularies to support multiple fields using the same taxonomy

See merge request !230
parents 0d38d10d 2ba987ae
Branches
Tags 5.x-1.1
No related merge requests found
Pipeline #480461 passed
......@@ -27,12 +27,6 @@ schemadotorg_taxonomy.settings:
group:
type: string
label: 'Group name'
schema_types:
type: sequence
label: 'Schema.org types'
sequence:
type: string
label: 'Schema.org type'
unlimited:
type: boolean
label: 'Unlimited values'
......@@ -51,6 +45,13 @@ schemadotorg_taxonomy.settings:
formatter_settings:
type: ignore
label: 'Formatter settings'
schema_types:
type: sequence
label: 'Schema.org types'
sequence:
type: string
label: 'Schema.org type'
schema_property_vocabularies:
type: sequence
label: 'Default properties'
......
......@@ -122,15 +122,21 @@ function schemadotorg_taxonomy_form_schemadotorg_types_settings_form_alter(array
'#title' => t('Default vocabularies'),
'#description' => t('Enter default vocabularies that will be added to every Schema.org content type.'),
'#example' => '
propertyName:
some_field_name:
id: vocabulary_id
label: Vocabulary name
description: Vocabulary description goes here
field_name: field_some_field_name
group: group_name
SchemaType--propertyName:
id: vocabulary_id
label: Vocabulary name
group: group_name
auto_create: true
unlimited: true
required: true
widget_id: widget_id
widget_settings: { }
formatter_id: formatter_id
formatter_settings: { }
schema_types:
- Article
',
];
......
......@@ -70,7 +70,7 @@ class SchemaDotOrgTaxonomyDefaultVocabularyManager implements SchemaDotOrgTaxono
$default_vocabularies = $this->configFactory->get('schemadotorg_taxonomy.settings')
->get('default_vocabularies');
foreach ($default_vocabularies as $vocabulary_id => $vocabulary_settings) {
foreach ($default_vocabularies as $field_name => $vocabulary_settings) {
$schema_types = $vocabulary_settings['schema_types'] ?? NULL;
// Check if the default vocabulary is for a specific Schema.org type.
if ($schema_types
......@@ -78,10 +78,10 @@ class SchemaDotOrgTaxonomyDefaultVocabularyManager implements SchemaDotOrgTaxono
continue;
}
// Make sure the vocabulary ID is a machine name.
$vocabulary_id = preg_replace('/[^a-z0-9_]+/', '_', $vocabulary_id);
$default_field_prefix = $this->configFactory->get('field_ui.settings')->get('field_prefix') ?? 'field_';
// Create vocabulary.
$vocabulary_id = $vocabulary_settings['id'] ?? $field_name;
$vocabulary = $this->createVocabulary($vocabulary_id, $vocabulary_settings);
$field = $vocabulary_settings + [
......@@ -92,7 +92,7 @@ class SchemaDotOrgTaxonomyDefaultVocabularyManager implements SchemaDotOrgTaxono
// Entity type and bundle.
'entity_type' => $entity_type_id,
'bundle' => $bundle,
'field_name' => 'field_' . $vocabulary_id,
'field_name' => $default_field_prefix . $field_name,
// Schema.org type and property.
'schema_type' => $mapping->getSchemaType(),
'schema_property' => '',
......
......@@ -63,6 +63,11 @@ class SchemaDotOrgTaxonomyDefaultVocabularyManagerKernelTest extends SchemaDotOr
'id' => 'tags',
'label' => 'Tags',
])
->set('default_vocabularies.tag', [
'id' => 'tags',
'label' => 'Tag',
'unlimited' => FALSE,
])
->set('default_vocabularies.article_tags', [
'id' => 'article_tags',
'label' => 'Article Tags',
......@@ -79,12 +84,22 @@ class SchemaDotOrgTaxonomyDefaultVocabularyManagerKernelTest extends SchemaDotOr
// Check that the field storage is created.
$this->assertNotNull(FieldStorageConfig::loadByName('node', 'field_tags'));
$this->assertNotNull(FieldStorageConfig::loadByName('node', 'field_tag'));
$this->assertNotNull(FieldStorageConfig::loadByName('node', 'field_article_tags'));
// Check that the field is created.
$this->assertNotNull(FieldConfig::loadByName('node', 'article', 'field_tags'));
$this->assertNotNull(FieldConfig::loadByName('node', 'article', 'field_tag'));
$this->assertNotNull(FieldConfig::loadByName('node', 'article', 'field_article_tags'));
// Check that field tag vs tags cardinality is set.
/** @var \Drupal\field\FieldStorageConfigInterface $field_storage */
$field_storage = FieldStorageConfig::loadByName('node', 'field_tag');
$this->assertEquals(1, $field_storage->getCardinality());
/** @var \Drupal\field\FieldStorageConfigInterface $field_storage */
$field_storage = FieldStorageConfig::loadByName('node', 'field_tags');
$this->assertEquals(-1, $field_storage->getCardinality());
/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository */
$entity_display_repository = \Drupal::service('entity_display.repository');
......@@ -98,7 +113,7 @@ class SchemaDotOrgTaxonomyDefaultVocabularyManagerKernelTest extends SchemaDotOr
$form_group = $form_display->getThirdPartySetting('field_group', 'group_taxonomy');
$this->assertEquals('Categories/Services', $form_group['label']);
$this->assertEquals('details', $form_group['format_type']);
$this->assertEquals(['field_tags', 'field_article_tags'], $form_group['children']);
$this->assertEquals(['field_tags', 'field_tag', 'field_article_tags'], $form_group['children']);
// Check that the view display and component are created.
$view_display = $entity_display_repository->getViewDisplay('node', 'article');
......@@ -110,7 +125,7 @@ class SchemaDotOrgTaxonomyDefaultVocabularyManagerKernelTest extends SchemaDotOr
$view_group = $view_display->getThirdPartySetting('field_group', 'group_taxonomy');
$this->assertEquals('Categories/Services', $view_group['label']);
$this->assertEquals('fieldset', $view_group['format_type']);
$this->assertEquals(['field_tags', 'field_article_tags'], $view_group['children']);
$this->assertEquals(['field_tags', 'field_tag', 'field_article_tags'], $view_group['children']);
// Check that tags and article_tags vocabularies are translated.
$this->assertNotNull(ContentLanguageSettings::load('taxonomy_term.tags'));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment