On this page
Advanced usage: alter comparable properties for existing field types
There are might be cases when you want, for example, compare text_with_summary
fields only by value
property (by default API compares it by format
and summary
properties too). Or otherwise you want to tell API that exactly this field type should be compared not only by value_1
but by value_2
too. In this case you need to override DefaultFieldComparator::extendComparableProperties(FieldDefinitionInterface $fieldDefinition, array $properties)
method.
Custom field comparator
We already know how to define custom field comparator from "Advanced usage: support for extra or custom fields" chapter. So here we will just consider how to force API to compare text_with_summary
fields only by its value. You need just to unset needed properties from passed array depending on a field type.
public function extendComparableProperties(FieldDefinitionInterface $fieldDefinition, array $properties) {
if ($fieldDefinition->getType() == 'text_with_summary') {
unset($properties[array_search('summary', $properties)]);
unset($properties[array_search('format', $properties)]);
}
return $properties;
}
When it's done changes in field summary
or format
properties will not be taken into consideration by the API.
Dependencies
Do not forget to include a file with defined observer and add dependency to Changed Fields API module to your *.info.yml
file:
dependencies:
- changed_fields
More info
See changed_fields_extended_field_comparator module for more information.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion