You are here

public function FieldManager::cloneField in Scheduled Updates 8

[@inheritdoc}

Overrides FieldManagerInterface::cloneField

File

src/FieldManager.php, line 60
Contains \Drupal\scheduled_updates\FieldManager.

Class

FieldManager
Field Manager for handling fields for Scheduled Updates.

Namespace

Drupal\scheduled_updates

Code

public function cloneField(ScheduledUpdateTypeInterface $scheduled_update_type, $field_name, $field_config_id = NULL, array $default_value = [], $hide = FALSE) {
  $entity_type = $scheduled_update_type
    ->getUpdateEntityType();
  $definition = $this
    ->getFieldStorageDefinition($entity_type, $field_name);
  if (!$definition) {
    return FALSE;
  }
  $new_field_name = $this
    ->getNewFieldName($definition);
  $field_storage_values = [
    'field_name' => $new_field_name,
    'entity_type' => 'scheduled_update',
    'type' => $definition
      ->getType(),
    'translatable' => $definition
      ->isTranslatable(),
    'settings' => $definition
      ->getSettings(),
    'cardinality' => $definition
      ->getCardinality(),
  ];
  $field_values = [
    'field_name' => $new_field_name,
    'entity_type' => 'scheduled_update',
    'bundle' => $scheduled_update_type
      ->id(),
    'label' => $definition
      ->getLabel(),
    // Field translatability should be explicitly enabled by the users.
    'translatable' => FALSE,
  ];

  /** @var FieldConfig $field_config */
  if ($field_config_id && ($field_config = FieldConfig::load($field_config_id))) {
    $field_values['settings'] = $field_config
      ->getSettings();
    $field_values['label'] = $field_config
      ->label();
  }

  // @todo Add Form display settings!
  FieldStorageConfig::create($field_storage_values)
    ->save();

  /** @var FieldConfigBase $field */
  $field = FieldConfig::create($field_values);
  $field
    ->save();
  if ($default_value) {
    $field
      ->setDefaultValue($default_value);
    $field
      ->save();
  }
  if (!$hide) {
    $this
      ->createFormDisplay($scheduled_update_type, $field_config_id, $definition, $new_field_name);
  }
  return $field;
}