You are here

public function MigrationSubscriber::onMigrationPostRowSave in Simplenews 3.x

Same name and namespace in other branches
  1. 8.2 src/EventSubscriber/MigrationSubscriber.php \Drupal\simplenews\EventSubscriber\MigrationSubscriber::onMigrationPostRowSave()
  2. 8 src/EventSubscriber/MigrationSubscriber.php \Drupal\simplenews\EventSubscriber\MigrationSubscriber::onMigrationPostRowSave()

Create simplenews field if applicable.

Parameters

\Drupal\migrate\Event\MigratePostRowSaveEvent $event: The event object.

File

src/EventSubscriber/MigrationSubscriber.php, line 58

Class

MigrationSubscriber
Create a simplenews field on relevant content types.

Namespace

Drupal\simplenews\EventSubscriber

Code

public function onMigrationPostRowSave(MigratePostRowSaveEvent $event) {
  if (!$event
    ->getRow()
    ->getSourceProperty('simplenews_content_type')) {
    return;
  }
  $node_type = reset($event
    ->getDestinationIdValues());
  $fields = $this->entityFieldManager
    ->getFieldDefinitions('node', $node_type);
  if (isset($fields['simplenews_issue'])) {
    return;
  }

  // If checked and the field does not exist yet, create it.
  $field_storage = FieldStorageConfig::loadByName('node', 'simplenews_issue');
  $field = FieldConfig::create([
    'field_storage' => $field_storage,
    'label' => $this
      ->t('Issue'),
    'bundle' => $node_type,
    'translatable' => TRUE,
  ]);
  $field
    ->save();

  // Set the default widget.
  $this->entityDisplayRepository
    ->getFormDisplay('node', $node_type)
    ->setComponent($field
    ->getName())
    ->save();
}