Pathologic was missing because of a .git folder inside.
[yaffs-website] / web / modules / contrib / diff / src / DiffEntityParser.php
1 <?php
2
3 namespace Drupal\diff;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Entity\ContentEntityInterface;
7 use Drupal\Core\Language\LanguageInterface;
8
9 /**
10  * Transforms an entity into an array of strings for diff.
11  */
12 class DiffEntityParser {
13
14   /**
15    * The diff field builder plugin manager.
16    *
17    * @var \Drupal\diff\DiffBuilderManager
18    */
19   protected $diffBuilderManager;
20
21   /**
22    * Wrapper object for simple configuration from diff.settings.yml.
23    *
24    * @var \Drupal\Core\Config\ImmutableConfig
25    */
26   protected $config;
27
28   /**
29    * Wrapper object for simple configuration from diff.plugins.yml.
30    *
31    * @var \Drupal\Core\Config\ImmutableConfig
32    */
33   protected $pluginsConfig;
34
35   /**
36    * Constructs a DiffEntityParser object.
37    *
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.
42    */
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;
47   }
48
49   /**
50    * Transforms an entity into an array of strings.
51    *
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
54    * of strings.
55    *
56    * @param \Drupal\Core\Entity\ContentEntityInterface $entity
57    *   An entity containing fields.
58    *
59    * @return array
60    *   Array of strings resulted by parsing the entity.
61    */
62   public function parseEntity(ContentEntityInterface $entity) {
63     $result = array();
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);
69     }
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')) {
78         continue;
79       }
80       // Create a plugin instance for the field definition.
81       $plugin = $this->diffBuilderManager->createInstanceForFieldDefinition($field_items->getFieldDefinition());
82       if ($plugin) {
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'];
90             };
91           }
92         }
93         else {
94           $build = $plugin->build($field_items);
95           if (!empty($build)) {
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();
98           }
99         }
100       }
101     }
102
103     $this->diffBuilderManager->clearCachedDefinitions();
104     return $result;
105   }
106
107 }