You are here

class FileFieldDiffParser in Entity Share 8.3

Plugin to diff file fields.

Plugin annotation


@DiffGenerator(
  id = "file_field_diff_parser",
  label = @Translation("File Field Diff Parser"),
  field_types = {
    "file"
  },
)

Hierarchy

Expanded class hierarchy of FileFieldDiffParser

File

modules/entity_share_diff/src/Plugin/DiffGenerator/FileFieldDiffParser.php, line 24

Namespace

Drupal\entity_share_diff\Plugin\DiffGenerator
View source
class FileFieldDiffParser extends DiffGeneratorPluginBase {

  /**
   * {@inheritdoc}
   */
  public function build(FieldItemListInterface $field_items, array $remote_field_data = []) {
    $result = [];
    $fileManager = $this->entityTypeManager
      ->getStorage('file');

    // Every item from $field_items is of type FieldItemInterface.
    if (!$this
      ->getRemote()) {
      foreach ($field_items as $field_key => $field_item) {

        // Even though the local field is empty, remote data may be set.
        // So, get field values regardless.
        $values = $field_item
          ->getValue();
        if (!$field_item
          ->isEmpty()) {

          // Compare file names.
          if (isset($values['target_id'])) {

            /** @var \Drupal\file\Entity\File $file */
            $file = $fileManager
              ->load($values['target_id']);
            if ($file instanceof File) {
              $label = (string) $this
                ->t('File name');
              $result[$field_key][$label] = $file
                ->getFilename();
              $label = (string) $this
                ->t('File size');
              $result[$field_key][$label] = $file
                ->getSize();
            }
          }
        }

        // Compare additional (meta) fields.
        foreach ($this
          ->getFieldMetaProperties() as $key => $label) {
          if (isset($values[$key])) {
            $result[$field_key][$label] = $values[$key];
          }
        }
      }
    }
    elseif (!empty($remote_field_data['data'])) {
      $detailed_response = $this->remoteManager
        ->jsonApiRequest($this
        ->getRemote(), 'GET', $remote_field_data['links']['related']['href']);
      $entities_json = Json::decode((string) $detailed_response
        ->getBody());
      if (!empty($entities_json['data'])) {
        $data = EntityShareUtility::prepareData($entities_json['data']);
      }
      else {
        $data = [];
      }
      foreach (array_keys($remote_field_data['data']) as $field_key) {
        if ($data[$field_key]['attributes']['filename']) {
          $label = (string) $this
            ->t('File name');
          $result[$field_key][$label] = $data[$field_key]['attributes']['filename'];
          $label = (string) $this
            ->t('File size');
          $result[$field_key][$label] = (string) $data[$field_key]['attributes']['filesize'];
        }

        // Compare additional (meta) fields.
        foreach ($this
          ->getFieldMetaProperties() as $key => $label) {
          if (isset($remote_field_data['data'][$field_key]['meta'][$key])) {
            $result[$field_key][$label] = $remote_field_data['data'][$field_key]['meta'][$key];
          }
        }
      }
    }
    return $result;
  }

  /**
   * Declares needed field meta properties.
   */
  protected function getFieldMetaProperties() {
    return [
      'description' => (string) $this
        ->t('Description'),
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DiffGeneratorPluginBase::$entityParser protected property The entity parser service.
DiffGeneratorPluginBase::$entityTypeManager protected property The entity type manager.
DiffGeneratorPluginBase::$remote protected property The 'Remote' entity.
DiffGeneratorPluginBase::$remoteManager protected property The remote manager service.
DiffGeneratorPluginBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
DiffGeneratorPluginBase::getRemote public function Returns Remote entity.
DiffGeneratorPluginBase::isApplicable public static function Returns if the plugin can be used for the provided field. Overrides DiffGeneratorInterface::isApplicable
DiffGeneratorPluginBase::setRemote public function Sets Remote entity.
DiffGeneratorPluginBase::__construct public function Constructs a DiffManagerBase object. Overrides PluginBase::__construct
FileFieldDiffParser::build public function Builds an array of strings. Overrides DiffGeneratorInterface::build
FileFieldDiffParser::getFieldMetaProperties protected function Declares needed field meta properties. 1
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.