class MigrationSubscriber in Simplenews 3.x
Same name and namespace in other branches
- 8.2 src/EventSubscriber/MigrationSubscriber.php \Drupal\simplenews\EventSubscriber\MigrationSubscriber
- 8 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
- class \Drupal\simplenews\EventSubscriber\MigrationSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses StringTranslationTrait
Expanded class hierarchy of MigrationSubscriber
1 string reference to 'MigrationSubscriber'
1 service uses MigrationSubscriber
File
- src/
EventSubscriber/ MigrationSubscriber.php, line 21
Namespace
Drupal\simplenews\EventSubscriberView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MigrationSubscriber:: |
protected | property | The entity display repository. | |
MigrationSubscriber:: |
protected | property | The entity field manager. | |
MigrationSubscriber:: |
public static | function | ||
MigrationSubscriber:: |
public | function | Create simplenews field if applicable. | |
MigrationSubscriber:: |
public | function | Constructs a new migration subscriber. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |