You are here

class MigrationSubscriber in Simplenews 8.2

Same name and namespace in other branches
  1. 8 src/EventSubscriber/MigrationSubscriber.php \Drupal\simplenews\EventSubscriber\MigrationSubscriber
  2. 3.x src/EventSubscriber/MigrationSubscriber.php \Drupal\simplenews\EventSubscriber\MigrationSubscriber

Create a simplenews field on relevant content types.

Since simplenews configuration on a node is stored as a field, it has to be added explicitly during a migration. This listener checks if the node type is a simplenews content type and adds the field.

Hierarchy

Expanded class hierarchy of MigrationSubscriber

1 string reference to 'MigrationSubscriber'
simplenews.services.yml in ./simplenews.services.yml
simplenews.services.yml
1 service uses MigrationSubscriber
simplenews.migration_subscriber in ./simplenews.services.yml
Drupal\simplenews\EventSubscriber\MigrationSubscriber

File

src/EventSubscriber/MigrationSubscriber.php, line 21

Namespace

Drupal\simplenews\EventSubscriber
View source
class MigrationSubscriber implements EventSubscriberInterface {
  use StringTranslationTrait;

  /**
   * The entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * The entity display repository.
   *
   * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
   */
  protected $entityDisplayRepository;

  /**
   * Constructs a new migration subscriber.
   *
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
   *   The entity field manager service.
   * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
   *   The entity display repository.
   */
  public function __construct(EntityFieldManagerInterface $entityFieldManager, EntityDisplayRepositoryInterface $entity_display_repository) {
    $this->entityFieldManager = $entityFieldManager;
    $this->entityDisplayRepository = $entity_display_repository;
  }

  /**
   * Create simplenews field if applicable.
   *
   * @param \Drupal\migrate\Event\MigratePostRowSaveEvent $event
   *   The event object.
   */
  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();
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $return = [];
    if (class_exists('\\Drupal\\migrate\\Event\\MigrateEvents')) {
      $return[MigrateEvents::POST_ROW_SAVE][] = 'onMigrationPostRowSave';
    }
    return $return;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrationSubscriber::$entityDisplayRepository protected property The entity display repository.
MigrationSubscriber::$entityFieldManager protected property The entity field manager.
MigrationSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
MigrationSubscriber::onMigrationPostRowSave public function Create simplenews field if applicable.
MigrationSubscriber::__construct public function Constructs a new migration subscriber.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.