class DefaultFieldComparator in Changed Fields API 8
Same name and namespace in other branches
- 8.3 src/Plugin/FieldComparator/DefaultFieldComparator.php \Drupal\changed_fields\Plugin\FieldComparator\DefaultFieldComparator
- 8.2 src/Plugin/FieldComparator/DefaultFieldComparator.php \Drupal\changed_fields\Plugin\FieldComparator\DefaultFieldComparator
Plugin annotation
@Plugin(
id = "default_field_comparator"
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\changed_fields\Plugin\FieldComparator\DefaultFieldComparator
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
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\FieldComparatorView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DefaultFieldComparator:: |
public | function | Method that compares old and new field values. | |
DefaultFieldComparator:: |
private | function | Method that returns comparable properties for existing field type. | |
DefaultFieldComparator:: |
protected | function | Method that returns comparable properties for extra or custom field type. | 1 |
DefaultFieldComparator:: |
private | function | Method that generates result array for DefaultFieldComparator::compareFieldValues(). | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |