You are here

public function UpdateService::update8803 in Apigee API Catalog 8.2

Recreate other fields added to API Doc entity onto the API Doc node type.

Return value

string A message to display.

File

src/UpdateService.php, line 183

Class

UpdateService
Class UpdateService.

Namespace

Drupal\apigee_api_catalog

Code

public function update8803() {
  $this->entityFieldManager
    ->clearCachedFieldDefinitions();
  $fieldStorageConfig = $this->entityTypeManager
    ->getStorage('field_storage_config');
  $fieldConfig = $this->entityTypeManager
    ->getStorage('field_config');

  // Get the last known state of the API Doc entity type.
  $apidocFieldDefs = $this->lastInstalledSchemaRepository
    ->getLastInstalledFieldStorageDefinitions('apidoc');
  foreach ($apidocFieldDefs as $fieldName => $definition) {

    // Only look for field starting with "field_", as those were added
    // through the UI.
    if (substr($fieldName, 0, 6) === 'field_') {

      // Namespace this custom field to avoid collisions.
      // Machine names have a maximum length of 32 characters https://www.drupal.org/node/2232665
      $newFieldName = substr($fieldName . '_apidoc', 0, 32);
      if (!FieldStorageConfig::loadByName('node', $newFieldName)) {
        $fieldStorageConfig
          ->create([
          'entity_type' => 'node',
          'field_name' => $newFieldName,
          'type' => $definition
            ->getType(),
          'cardinality' => $definition
            ->getCardinality(),
          'settings' => $definition
            ->getSettings(),
          'label' => $definition
            ->getLabel(),
        ])
          ->save();
      }
      if (!FieldConfig::loadByName('node', 'apidoc', $newFieldName)) {
        $fieldConfig
          ->create([
          'field_name' => $newFieldName,
          'entity_type' => 'node',
          'bundle' => 'apidoc',
          'label' => $definition
            ->getLabel(),
          'description' => $definition
            ->getDescription(),
          'settings' => $definition
            ->getSettings(),
        ])
          ->save();
      }
      $this
        ->addToFieldMap($fieldName, $newFieldName);
    }
  }
  return 'Recreated custom fields (if any) added to the API Doc entity onto the API Doc node type.';
}