<?php | |
namespace Drupal\up_migrate\Plugin\migrate\source; | |
use ArrayObject; | |
use Drupal\Core\StringTranslation\StringTranslationTrait; | |
use Drupal\migrate\Plugin\migrate\source\SourcePluginBase; | |
use Drupal\migrate\Plugin\MigrationInterface; | |
use Drupal\paragraphs\Entity\ParagraphsType; | |
/** | |
* Source plugin for ParagraphsType. | |
* | |
* @MigrateSource( | |
* id = "up_paragraphs_type", | |
* source_module = "up_migrate" | |
* ) | |
*/ | |
class UpParagraphsType extends SourcePluginBase { | |
use StringTranslationTrait { | |
t as t_original; | |
} | |
/** | |
* List of paragraphs types to exclude. | |
* | |
* @var array | |
*/ | |
protected $exclude = []; | |
/** | |
* List of paragraph types. | |
* | |
* @var array | |
*/ | |
protected $items = []; | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function t($string, array $args = [], array $options = []) { | |
if (empty($options['context'])) { | |
$options['context'] = 'up_migrate'; | |
} | |
return $this->t_original($string, $args, $options); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration) { | |
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration); | |
if (isset($configuration['exclude'])) { | |
$this->exclude = $configuration['exclude']; | |
} | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function fields() { | |
return [ | |
'id' => $this->t('ID'), | |
'label' => $this->t('Label'), | |
]; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getIds() { | |
$ids['id']['type'] = 'string'; | |
return $ids; | |
} | |
/** | |
* Return a comma-separated list of paragraph type ids. | |
*/ | |
public function __toString() { | |
return implode(', ', array_column($this->items, 'id')); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function initializeIterator() { | |
$this->items = []; | |
$paragraphs_types = ParagraphsType::loadMultiple(); | |
/** @var \Drupal\paragraphs\ParagraphsTypeInterface $paragraphs_type */ | |
foreach ($paragraphs_types as $paragraphs_type) { | |
$this->items[$paragraphs_type->id()] = [ | |
'id' => $paragraphs_type->id(), | |
'label' => $paragraphs_type->label(), | |
]; | |
} | |
if (!empty($this->exclude)) { | |
$this->items = array_diff_key($this->items, array_flip($this->exclude)); | |
} | |
return (new ArrayObject($this->items))->getIterator(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function count($refresh = FALSE) { | |
parent::count($this->items); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment