Skip to content
Snippets Groups Projects

Issue #3076862 by simeonkesmev: Avoid depending on 'field_' field name prefix for determining bundle fields

Closed Issue #3076862 by simeonkesmev: Avoid depending on 'field_' field name prefix for determining bundle fields
Closed M Parker requested to merge 8.x-1.x into 2.x
2 files
+ 43
20
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -6,20 +6,37 @@ use Drupal\Core\Controller\ControllerBase;
@@ -6,20 +6,37 @@ use Drupal\Core\Controller\ControllerBase;
use Drupal\structure_sync\StructureSyncHelper;
use Drupal\structure_sync\StructureSyncHelper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Term;
 
use Drupal\Core\Entity\EntityFieldManagerInterface;
 
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
/**
* Controller for syncing taxonomy terms.
* Controller for syncing taxonomy terms.
*/
*/
class TaxonomiesController extends ControllerBase {
class TaxonomiesController extends ControllerBase {
 
/**
 
* The entity field manager.
 
*
 
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
 
*/
 
protected $entityFieldManager;
 
private $config;
private $config;
/**
/**
* Constructor for taxonomies controller.
* Constructor for taxonomies controller.
*/
*/
public function __construct() {
public function __construct(EntityFieldManagerInterface $entity_field_manager) {
$this->config = $this->getEditableConfig();
$this->config = $this->getEditableConfig();
$this->entityTypeManager();
$this->entityTypeManager();
 
$this->entityFieldManager = $entity_field_manager;
 
}
 
 
/**
 
* {@inheritdoc}
 
*/
 
public static function create(ContainerInterface $container) {
 
return new static($container->get('entity_field.manager'));
}
}
/**
/**
@@ -101,8 +118,7 @@ class TaxonomiesController extends ControllerBase {
@@ -101,8 +118,7 @@ class TaxonomiesController extends ControllerBase {
$entity_field_names = [];
$entity_field_names = [];
$all_term_fields = $entity->getFields();
$all_term_fields = $entity->getFields();
foreach ($all_term_fields as $field_name => $field) {
foreach ($all_term_fields as $field_name => $field) {
$is_custom_field = 'field_' === substr($field_name, 0, 6);
if ($this->isCustomField($vocabulary, $field_name)) {
if ($is_custom_field) {
$entity_field_names[] = $field_name;
$entity_field_names[] = $field_name;
}
}
}
}
@@ -212,18 +228,18 @@ class TaxonomiesController extends ControllerBase {
@@ -212,18 +228,18 @@ class TaxonomiesController extends ControllerBase {
switch ($style) {
switch ($style) {
case 'full':
case 'full':
self::deleteDeletedTaxonomies($taxonomies, $context);
self::deleteDeletedTaxonomies($taxonomies, $context);
self::importTaxonomiesFull($taxonomies, $context);
$this->importTaxonomiesFull($taxonomies, $context);
self::taxonomiesImportFinishedCallback(NULL, NULL, NULL);
self::taxonomiesImportFinishedCallback(NULL, NULL, NULL);
break;
break;
case 'safe':
case 'safe':
self::importTaxonomiesSafe($taxonomies, $context);
$this->importTaxonomiesSafe($taxonomies, $context);
self::taxonomiesImportFinishedCallback(NULL, NULL, NULL);
self::taxonomiesImportFinishedCallback(NULL, NULL, NULL);
break;
break;
case 'force':
case 'force':
self::deleteTaxonomies($context);
self::deleteTaxonomies($context);
self::importTaxonomiesForce($taxonomies, $context);
$this->importTaxonomiesForce($taxonomies, $context);
self::taxonomiesImportFinishedCallback(NULL, NULL, NULL);
self::taxonomiesImportFinishedCallback(NULL, NULL, NULL);
break;
break;
}
}
@@ -322,7 +338,7 @@ class TaxonomiesController extends ControllerBase {
@@ -322,7 +338,7 @@ class TaxonomiesController extends ControllerBase {
* Basically a safe import with update actions for already existing taxonomy
* Basically a safe import with update actions for already existing taxonomy
* terms.
* terms.
*/
*/
public static function importTaxonomiesFull($taxonomies, &$context) {
public function importTaxonomiesFull($taxonomies, &$context) {
$uuidsInConfig = [];
$uuidsInConfig = [];
foreach ($taxonomies as $voc) {
foreach ($taxonomies as $voc) {
foreach ($voc as $taxonomy) {
foreach ($voc as $taxonomy) {
@@ -363,8 +379,7 @@ class TaxonomiesController extends ControllerBase {
@@ -363,8 +379,7 @@ class TaxonomiesController extends ControllerBase {
// terms.
// terms.
$entity_fields = [];
$entity_fields = [];
foreach ($taxonomy as $field_name => $field_value) {
foreach ($taxonomy as $field_name => $field_value) {
$is_custom_field = 'field_' === substr($field_name, 0, 6);
if ($this->isCustomField($vid, $field_name)) {
if ($is_custom_field) {
$not_term_reference = empty($field_value[0]['vid']);
$not_term_reference = empty($field_value[0]['vid']);
if ($not_term_reference) {
if ($not_term_reference) {
@@ -486,7 +501,7 @@ class TaxonomiesController extends ControllerBase {
@@ -486,7 +501,7 @@ class TaxonomiesController extends ControllerBase {
StructureSyncHelper::logMessage('Running additional full import'
StructureSyncHelper::logMessage('Running additional full import'
. ' after all terms have been created in order to identify missing '
. ' after all terms have been created in order to identify missing '
. ' TIDs for term reference fields.');
. ' TIDs for term reference fields.');
Self::importTaxonomiesFull($taxonomies, $context);
$this->importTaxonomiesFull($taxonomies, $context);
}
}
$firstRun = FALSE;
$firstRun = FALSE;
@@ -510,7 +525,7 @@ class TaxonomiesController extends ControllerBase {
@@ -510,7 +525,7 @@ class TaxonomiesController extends ControllerBase {
* Safely meaning that it should only add what isn't already there and not
* Safely meaning that it should only add what isn't already there and not
* delete and/or update any terms.
* delete and/or update any terms.
*/
*/
public static function importTaxonomiesSafe($taxonomies, &$context) {
public function importTaxonomiesSafe($taxonomies, &$context) {
$tidsDone = [];
$tidsDone = [];
$tidsLeft = [];
$tidsLeft = [];
$newTids = [];
$newTids = [];
@@ -553,8 +568,7 @@ class TaxonomiesController extends ControllerBase {
@@ -553,8 +568,7 @@ class TaxonomiesController extends ControllerBase {
// terms.
// terms.
$entity_fields = [];
$entity_fields = [];
foreach ($taxonomy as $field_name => $field_value) {
foreach ($taxonomy as $field_name => $field_value) {
$is_custom_field = 'field_' === substr($field_name, 0, 6);
if ($this->isCustomField($vid, $field_name)) {
if ($is_custom_field) {
$not_term_reference = empty($field_value[0]['vid']);
$not_term_reference = empty($field_value[0]['vid']);
if ($not_term_reference) {
if ($not_term_reference) {
@@ -654,7 +668,7 @@ class TaxonomiesController extends ControllerBase {
@@ -654,7 +668,7 @@ class TaxonomiesController extends ControllerBase {
StructureSyncHelper::logMessage('Running additional full import'
StructureSyncHelper::logMessage('Running additional full import'
. ' after all terms have been created in order to identify missing '
. ' after all terms have been created in order to identify missing '
. ' TIDs for term reference fields.');
. ' TIDs for term reference fields.');
Self::importTaxonomiesFull($taxonomies, $context);
$this->importTaxonomiesFull($taxonomies, $context);
}
}
$firstRun = FALSE;
$firstRun = FALSE;
@@ -683,7 +697,7 @@ class TaxonomiesController extends ControllerBase {
@@ -683,7 +697,7 @@ class TaxonomiesController extends ControllerBase {
/**
/**
* Function to import (create) all taxonomies that need to be imported.
* Function to import (create) all taxonomies that need to be imported.
*/
*/
public static function importTaxonomiesForce($taxonomies, &$context) {
public function importTaxonomiesForce($taxonomies, &$context) {
$tidsDone = [];
$tidsDone = [];
$tidsLeft = [];
$tidsLeft = [];
$newTids = [];
$newTids = [];
@@ -720,8 +734,7 @@ class TaxonomiesController extends ControllerBase {
@@ -720,8 +734,7 @@ class TaxonomiesController extends ControllerBase {
// terms.
// terms.
$entity_fields = [];
$entity_fields = [];
foreach ($taxonomy as $field_name => $field_value) {
foreach ($taxonomy as $field_name => $field_value) {
$is_custom_field = 'field_' === substr($field_name, 0, 6);
if ($this->isCustomField($vid, $field_name)) {
if ($is_custom_field) {
$not_term_reference = empty($field_value[0]['vid']);
$not_term_reference = empty($field_value[0]['vid']);
if ($not_term_reference) {
if ($not_term_reference) {
@@ -804,7 +817,7 @@ class TaxonomiesController extends ControllerBase {
@@ -804,7 +817,7 @@ class TaxonomiesController extends ControllerBase {
StructureSyncHelper::logMessage('Running additional full import'
StructureSyncHelper::logMessage('Running additional full import'
. ' after all terms have been created in order to identify missing '
. ' after all terms have been created in order to identify missing '
. ' TIDs for term reference fields.');
. ' TIDs for term reference fields.');
Self::importTaxonomiesFull($taxonomies, $context);
$this->importTaxonomiesFull($taxonomies, $context);
}
}
$firstRun = FALSE;
$firstRun = FALSE;
@@ -822,4 +835,14 @@ class TaxonomiesController extends ControllerBase {
@@ -822,4 +835,14 @@ class TaxonomiesController extends ControllerBase {
drupal_set_message(t('Successfully imported taxonomies'));
drupal_set_message(t('Successfully imported taxonomies'));
}
}
 
/**
 
* Determines if the field name is a custom field.
 
*/
 
private function isCustomField($entity_bundle_id, $field_name) {
 
$base_field_definitions = $this->entityFieldManager->getBaseFieldDefinitions('taxonomy_term');
 
$field_definitions = $this->entityFieldManager->getFieldDefinitions('taxonomy_term', $entity_bundle_id);
 
 
return isset($field_definitions[$field_name]) && !isset($base_field_definitions[$field_name]);
 
}
 
}
}
Loading