Advanced usage: alter comparable properties for existing field types

Last updated on
29 July 2017

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

Page status: No known problems

You can: