5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Entity\ContentEntityInterface;
7 use Drupal\Core\Language\LanguageInterface;
10 * Transforms an entity into an array of strings for diff.
12 class DiffEntityParser {
15 * The diff field builder plugin manager.
17 * @var \Drupal\diff\DiffBuilderManager
19 protected $diffBuilderManager;
22 * Wrapper object for simple configuration from diff.settings.yml.
24 * @var \Drupal\Core\Config\ImmutableConfig
29 * Wrapper object for simple configuration from diff.plugins.yml.
31 * @var \Drupal\Core\Config\ImmutableConfig
33 protected $pluginsConfig;
36 * Constructs a DiffEntityParser object.
38 * @param \Drupal\diff\DiffBuilderManager $diff_builder_manager
39 * The diff builder manager.
40 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
41 * The configuration factory.
43 public function __construct(DiffBuilderManager $diff_builder_manager, ConfigFactoryInterface $config_factory) {
44 $this->config = $config_factory->get('diff.settings');
45 $this->pluginsConfig = $config_factory->get('diff.plugins');
46 $this->diffBuilderManager = $diff_builder_manager;
50 * Transforms an entity into an array of strings.
52 * Parses an entity's fields and for every field it builds an array of string
53 * to be compared. Basically this function transforms an entity into an array
56 * @param \Drupal\Core\Entity\ContentEntityInterface $entity
57 * An entity containing fields.
60 * Array of strings resulted by parsing the entity.
62 public function parseEntity(ContentEntityInterface $entity) {
64 $langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
65 // Load entity of current language, otherwise fields are always compared by
66 // their default language.
67 if ($entity->hasTranslation($langcode)) {
68 $entity = $entity->getTranslation($langcode);
70 $entity_type_id = $entity->getEntityTypeId();
71 // Loop through entity fields and transform every FieldItemList object
72 // into an array of strings according to field type specific settings.
73 /** @var \Drupal\Core\Field\FieldItemListInterface $field_items */
74 foreach ($entity as $field_items) {
75 // Define if the current field should be displayed as a diff change.
76 $show_diff = $this->diffBuilderManager->showDiff($field_items->getFieldDefinition()->getFieldStorageDefinition());
77 if (!$show_diff || !$entity->get($field_items->getFieldDefinition()->getName())->access('view')) {
80 // Create a plugin instance for the field definition.
81 $plugin = $this->diffBuilderManager->createInstanceForFieldDefinition($field_items->getFieldDefinition());
83 // Create the array with the fields of the entity. Recursive if the
84 // field contains entities.
85 if ($plugin instanceof FieldReferenceInterface) {
86 foreach ($plugin->getEntitiesToDiff($field_items) as $entity_key => $reference_entity) {
87 foreach ($this->parseEntity($reference_entity) as $key => $build) {
88 $result[$key] = $build;
89 $result[$key]['label'] = $field_items->getFieldDefinition()->getLabel() . ' > ' . $result[$key]['label'];
94 $build = $plugin->build($field_items);
96 $result[$entity->id() . ':' . $entity_type_id . '.' . $field_items->getName()] = $build;
97 $result[$entity->id() . ':' . $entity_type_id . '.' . $field_items->getName()]['label'] = $field_items->getFieldDefinition()->getLabel();
103 $this->diffBuilderManager->clearCachedDefinitions();