X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fdiff%2Fsrc%2FDiffEntityParser.php;fp=web%2Fmodules%2Fcontrib%2Fdiff%2Fsrc%2FDiffEntityParser.php;h=bc41aa241847f8511f7c6dbf6123523fc1b38b7c;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/modules/contrib/diff/src/DiffEntityParser.php b/web/modules/contrib/diff/src/DiffEntityParser.php new file mode 100644 index 000000000..bc41aa241 --- /dev/null +++ b/web/modules/contrib/diff/src/DiffEntityParser.php @@ -0,0 +1,107 @@ +config = $config_factory->get('diff.settings'); + $this->pluginsConfig = $config_factory->get('diff.plugins'); + $this->diffBuilderManager = $diff_builder_manager; + } + + /** + * Transforms an entity into an array of strings. + * + * Parses an entity's fields and for every field it builds an array of string + * to be compared. Basically this function transforms an entity into an array + * of strings. + * + * @param \Drupal\Core\Entity\ContentEntityInterface $entity + * An entity containing fields. + * + * @return array + * Array of strings resulted by parsing the entity. + */ + public function parseEntity(ContentEntityInterface $entity) { + $result = array(); + $langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId(); + // Load entity of current language, otherwise fields are always compared by + // their default language. + if ($entity->hasTranslation($langcode)) { + $entity = $entity->getTranslation($langcode); + } + $entity_type_id = $entity->getEntityTypeId(); + // Loop through entity fields and transform every FieldItemList object + // into an array of strings according to field type specific settings. + /** @var \Drupal\Core\Field\FieldItemListInterface $field_items */ + foreach ($entity as $field_items) { + // Define if the current field should be displayed as a diff change. + $show_diff = $this->diffBuilderManager->showDiff($field_items->getFieldDefinition()->getFieldStorageDefinition()); + if (!$show_diff || !$entity->get($field_items->getFieldDefinition()->getName())->access('view')) { + continue; + } + // Create a plugin instance for the field definition. + $plugin = $this->diffBuilderManager->createInstanceForFieldDefinition($field_items->getFieldDefinition()); + if ($plugin) { + // Create the array with the fields of the entity. Recursive if the + // field contains entities. + if ($plugin instanceof FieldReferenceInterface) { + foreach ($plugin->getEntitiesToDiff($field_items) as $entity_key => $reference_entity) { + foreach ($this->parseEntity($reference_entity) as $key => $build) { + $result[$key] = $build; + $result[$key]['label'] = $field_items->getFieldDefinition()->getLabel() . ' > ' . $result[$key]['label']; + }; + } + } + else { + $build = $plugin->build($field_items); + if (!empty($build)) { + $result[$entity->id() . ':' . $entity_type_id . '.' . $field_items->getName()] = $build; + $result[$entity->id() . ':' . $entity_type_id . '.' . $field_items->getName()]['label'] = $field_items->getFieldDefinition()->getLabel(); + } + } + } + } + + $this->diffBuilderManager->clearCachedDefinitions(); + return $result; + } + +}