You are here

class DiffEntityParser in Diff 8

Transforms an entity into an array of strings for diff.

Hierarchy

Expanded class hierarchy of DiffEntityParser

4 files declare their use of DiffEntityParser
CoreFieldBuilder.php in src/Plugin/diff/Field/CoreFieldBuilder.php
SplitFieldsDiffLayout.php in src/Plugin/diff/Layout/SplitFieldsDiffLayout.php
UnifiedFieldsDiffLayout.php in src/Plugin/diff/Layout/UnifiedFieldsDiffLayout.php
VisualInlineDiffLayout.php in src/Plugin/diff/Layout/VisualInlineDiffLayout.php
1 string reference to 'DiffEntityParser'
diff.services.yml in ./diff.services.yml
diff.services.yml
1 service uses DiffEntityParser
diff.entity_parser in ./diff.services.yml
Drupal\diff\DiffEntityParser

File

src/DiffEntityParser.php, line 12

Namespace

Drupal\diff
View source
class DiffEntityParser {

  /**
   * The diff field builder plugin manager.
   *
   * @var \Drupal\diff\DiffBuilderManager
   */
  protected $diffBuilderManager;

  /**
   * Wrapper object for simple configuration from diff.settings.yml.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * Wrapper object for simple configuration from diff.plugins.yml.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $pluginsConfig;

  /**
   * Constructs a DiffEntityParser object.
   *
   * @param \Drupal\diff\DiffBuilderManager $diff_builder_manager
   *   The diff builder manager.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   */
  public function __construct(DiffBuilderManager $diff_builder_manager, ConfigFactoryInterface $config_factory) {
    $this->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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DiffEntityParser::$config protected property Wrapper object for simple configuration from diff.settings.yml.
DiffEntityParser::$diffBuilderManager protected property The diff field builder plugin manager.
DiffEntityParser::$pluginsConfig protected property Wrapper object for simple configuration from diff.plugins.yml.
DiffEntityParser::parseEntity public function Transforms an entity into an array of strings.
DiffEntityParser::__construct public function Constructs a DiffEntityParser object.