You are here

public function Replicator::postCloneEntityField in Replicate 8

Postprocess a cloned entity field.

A public API method so modules can e.g. clone a field partially.

Parameters

\Drupal\Core\Field\FieldItemListInterface $target_field: The cloned field.

1 call to Replicator::postCloneEntityField()
Replicator::cloneEntityField in src/Replicator.php
Clone an entity field to another.

File

src/Replicator.php, line 182

Class

Replicator
Class Replicator. Manages the replication of an entity.

Namespace

Drupal\replicate

Code

public function postCloneEntityField(FieldItemListInterface $target_field) {
  $entity = $target_field
    ->getEntity();
  if (!$entity instanceof FieldableEntityInterface) {

    // @todo Can this ever happen? The interface only assures EntityInterface.
    throw new FieldException($this
      ->t('Trying to clone into non fieldable Entity.'));
  }

  /** @var FieldableEntityInterface $entity */
  $violations = $target_field
    ->validate();
  if ($violations
    ->count()) {

    // This autocasts violations to string.
    $t_args = [
      '%violations' => implode(' & ', $violations),
    ];
    throw new FieldException($this
      ->t('Trying to clone into incompatible field: %violations', $t_args));
  }
  $this
    ->dispatchEventCloneEntityField($entity, $target_field
    ->getName(), $target_field
    ->getFieldDefinition());
}