b6d60b913470bca9e402639dc229156e629b7346
[yaffs-website] / web / core / modules / comment / src / Plugin / migrate / destination / EntityComment.php
1 <?php
2
3 namespace Drupal\comment\Plugin\migrate\destination;
4
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\Field\FieldTypePluginManagerInterface;
8 use Drupal\Core\State\StateInterface;
9 use Drupal\migrate\Plugin\MigrationInterface;
10 use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
11 use Drupal\migrate\Row;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * @MigrateDestination(
16  *   id = "entity:comment"
17  * )
18  */
19 class EntityComment extends EntityContentBase {
20
21   /**
22    * The state storage object.
23    *
24    * @var \Drupal\Core\State\StateInterface
25    */
26   protected $state;
27
28   /**
29    * An array of entity IDs for the 'commented entity' keyed by entity type.
30    *
31    * @var array
32    */
33   protected $stubCommentedEntityIds;
34
35   /**
36    * Builds an comment entity destination.
37    *
38    * @param array $configuration
39    *   A configuration array containing information about the plugin instance.
40    * @param string $plugin_id
41    *   The plugin_id for the plugin instance.
42    * @param mixed $plugin_definition
43    *   The plugin implementation definition.
44    * @param \Drupal\migrate\Plugin\MigrationInterface $migration
45    *   The migration.
46    * @param \Drupal\Core\Entity\EntityStorageInterface $storage
47    *   The storage for this entity type.
48    * @param array $bundles
49    *   The list of bundles this entity type has.
50    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
51    *   The entity manager service.
52    * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
53    *   The field type plugin manager service.
54    * @param \Drupal\Core\State\StateInterface $state
55    *   The state storage object.
56    */
57   public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager, StateInterface $state) {
58     parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles, $entity_manager, $field_type_manager);
59     $this->state = $state;
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
66     $entity_type = static::getEntityTypeId($plugin_id);
67     return new static(
68       $configuration,
69       $plugin_id,
70       $plugin_definition,
71       $migration,
72       $container->get('entity.manager')->getStorage($entity_type),
73       array_keys($container->get('entity.manager')->getBundleInfo($entity_type)),
74       $container->get('entity.manager'),
75       $container->get('plugin.manager.field.field_type'),
76       $container->get('state')
77     );
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public function import(Row $row, array $old_destination_id_values = []) {
84     if ($row->isStub() && ($state = $this->state->get('comment.maintain_entity_statistics', 0))) {
85       $this->state->set('comment.maintain_entity_statistics', 0);
86     }
87     $return = parent::import($row, $old_destination_id_values);
88     if ($row->isStub() && $state) {
89       $this->state->set('comment.maintain_entity_statistics', $state);
90     }
91     return $return;
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   protected function processStubRow(Row $row) {
98     parent::processStubRow($row);
99     // Neither uid nor name is required in itself, but it is required to set one
100     // of them.
101     $row->setDestinationProperty('name', 'anonymous_stub');
102   }
103
104 }