setFieldMapperId should reset fieldMapperPlugin member

Created on 4 March 2024, 4 months ago
Updated 20 May 2024, about 1 month ago

Problem/Motivation

In ExternalEntityType class, the public method setFieldMapperId() (currently only used in tests in this module) changes the field mapper plugin id while it does not reset the fieldMapperPlugin member which may have been initialized with a different field mapper id:

  /**
   * {@inheritdoc}
   */
  public function setFieldMapperId($field_mapper_id) {
    $this->field_mapper_id = $field_mapper_id;
  }

  /**
   * {@inheritdoc}
   */
  public function getFieldMapper() {
    if (!$this->fieldMapperPlugin) {
      $field_mapper_plugin_manager =
        \Drupal::service('plugin.manager.external_entities.field_mapper');
      assert($field_mapper_plugin_manager instanceof FieldMapperManager);

      $config = $this->getFieldMapperConfig();

      // Allow the mapper to call back into the entity type (e.g., to fetch
      // additional data like field lists from the remote service).
      $config['_external_entity_type'] = $this;

      $this->fieldMapperPlugin =
        $field_mapper_plugin_manager->createInstance(
          $this->getFieldMapperId(),
          $config
        );
    }

    return $this->fieldMapperPlugin;
  }

Therefore, a developer may think calling setFieldMapperId() changes the field mapper but it does not and there can be a discrepancy between what is returned by ->getFieldMapperId() and ->getFieldMapper()->getPluginId() which could lead to unexpected behaviors. As well, the field mapper config is not changed and may not correspond to the field mapper.

Proposed resolution

  /**
   * {@inheritdoc}
   */
  public function setFieldMapperId($field_mapper_id) {
    $this->field_mapper_id = $field_mapper_id;
    $this->fieldMapperPlugin = NULL;
    $this->field_mapper_config = [];
  }

Remaining tasks

Review the logic and merge.

User interface changes

None.

API changes

None.

Data model changes

None.

πŸ› Bug report
Status

Fixed

Version

3.0

Component

Code

Created by

πŸ‡«πŸ‡·France guignonv

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.69.0 2024