You are here

class DefaultFieldComparator in Changed Fields API 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/FieldComparator/DefaultFieldComparator.php \Drupal\changed_fields\Plugin\FieldComparator\DefaultFieldComparator
  2. 8.2 src/Plugin/FieldComparator/DefaultFieldComparator.php \Drupal\changed_fields\Plugin\FieldComparator\DefaultFieldComparator

Plugin annotation


@Plugin(
  id = "default_field_comparator"
)

Hierarchy

Expanded class hierarchy of DefaultFieldComparator

1 file declares its use of DefaultFieldComparator
ExtendedFieldComparator.php in examples/changed_fields_extended_field_comparator/src/Plugin/FieldComparator/ExtendedFieldComparator.php
Contains ExtendedFieldComparator.php.

File

src/Plugin/FieldComparator/DefaultFieldComparator.php, line 19
Contains DefaultFieldComparator.php.

Namespace

Drupal\changed_fields\Plugin\FieldComparator
View source
class DefaultFieldComparator extends PluginBase {

  /**
   * Method that returns comparable properties for existing field type.
   *
   * @param FieldDefinitionInterface $fieldDefinition
   * @return array
   */
  private function getComparableProperties(FieldDefinitionInterface $fieldDefinition) {
    switch ($fieldDefinition
      ->getType()) {
      case 'string':
      case 'string_long':
      case 'text':
      case 'text_long':
      case 'boolean':
      case 'integer':
      case 'float':
      case 'decimal':
      case 'datetime':
      case 'email':
      case 'list_integer':
      case 'list_float':
      case 'list_string':
      case 'telephone':
        $properties = array(
          'value',
        );
        break;
      case 'text_with_summary':
        $properties = array(
          'value',
          'summary',
        );
        break;
      case 'entity_reference':
        $properties = array(
          'target_id',
        );
        break;
      case 'file':
        $properties = array(
          'target_id',
          'description',
        );
        break;
      case 'image':
        $properties = array(
          'fid',
          'width',
          'height',
          'target_id',
          'alt',
          'title',
        );
        break;
      case 'link':
        $properties = array(
          'uri',
          'title',
        );
        break;
      default:
        $properties = $this
          ->getDefaultComparableProperties($fieldDefinition);
        break;
    }
    return $properties;
  }

  /**
   * Method that returns comparable properties for extra or custom field type.
   *
   * Use it if you want to add comparison support
   * for extra or custom field types.
   *
   * @param FieldDefinitionInterface $fieldDefinition
   * @return array
   */
  protected function getDefaultComparableProperties(FieldDefinitionInterface $fieldDefinition) {
    return array();
  }

  /**
   * Method that compares old and new field values.
   *
   * @param FieldDefinitionInterface $fieldDefinition
   * @param array $oldValue
   * @param array $newValue
   * @return array|bool
   */
  public function compareFieldValues(FieldDefinitionInterface $fieldDefinition, array $oldValue, array $newValue) {
    $result = TRUE;
    $properties = $this
      ->getComparableProperties($fieldDefinition);

    // If value was added or removed then we have already different values.
    if (!$oldValue && $newValue || $oldValue && !$newValue) {
      $result = $this
        ->makeResultArray($oldValue, $newValue);
    }
    else {
      if ($oldValue && $newValue) {

        // If value was added|removed to|from multi-value field then we have
        // already different values.
        if (count($newValue) != count($oldValue)) {
          $result = $this
            ->makeResultArray($oldValue, $newValue);
        }
        else {

          // Walk through each field value and compare it's properties.
          foreach ($newValue as $key => $value) {
            if (is_array($result)) {
              break;
            }
            foreach ($properties as $property) {
              if ($newValue[$key][$property] != $oldValue[$key][$property]) {
                $result = $this
                  ->makeResultArray($oldValue, $newValue);
                break;
              }
            }
          }
        }
      }
    }
    return $result;
  }

  /**
   * Method that generates result array for DefaultFieldComparator::compareFieldValues().
   *
   * @param array $oldValue
   * @param array $newValue
   * @return array
   */
  private function makeResultArray(array $oldValue, array $newValue) {
    return array(
      'old_value' => $oldValue,
      'new_value' => $newValue,
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultFieldComparator::compareFieldValues public function Method that compares old and new field values.
DefaultFieldComparator::getComparableProperties private function Method that returns comparable properties for existing field type.
DefaultFieldComparator::getDefaultComparableProperties protected function Method that returns comparable properties for extra or custom field type. 1
DefaultFieldComparator::makeResultArray private function Method that generates result array for DefaultFieldComparator::compareFieldValues().
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
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.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
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.