paragraphStorage = $entity_type_manager->getStorage('paragraph');
$this->paragraphsTypeStorage = $entity_type_manager->getStorage('paragraphs_type');
$this->fieldConfigStorage = $entity_type_manager->getStorage('field_config');
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'paragraphs_type' => NULL,
'paragraph_field' => NULL,
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['paragraphs_type'] = [
'#type' => 'select',
'#title' => $this->t('Paragraphs type'),
'#required' => TRUE,
'#options' => array_map(function(EntityInterface $paragraphs_type) {
return $paragraphs_type->label();
}, $this->paragraphsTypeStorage->loadMultiple()),
'#default_value' => $this->configuration['paragraphs_type'],
];
// Load and filter field configs to create options.
/** @var \Drupal\field\FieldConfigInterface[] $field_configs */
$field_configs = $this->fieldConfigStorage->loadByProperties([
'entity_type' => 'paragraph',
'bundle' => $this->configuration['paragraphs_type'],
]);
$field_options = [];
foreach ($field_configs as $field_config) {
if (in_array($field_config->getType(), ['text', 'text_long', 'text_with_summary'])) {
$field_options[$field_config->getName()] = $field_config->label();
}
}
$form['paragraph_field'] = [
'#type' => 'select',
'#title' => $this->t('Paragraph field'),
'#description' => $this->t('Note: Field options do not appear until a type has been chosen and saved.'),
'#options' => $field_options,
];
$form = parent::buildConfigurationForm($form, $form_state);
return $form;
}
/**
* {@inheritdoc}
*/
public function getSummary() {
$summary = $this->t('Not yet configured.');
$paragraphs_type_id = $this->configuration['paragraphs_type'];
$paragraph_field_name = $this->configuration['paragraph_field'];
if ($paragraphs_type_id && $paragraphs_type = $this->paragraphsTypeStorage->load($paragraphs_type_id)) {
if ($paragraph_field_name && $paragraph_field = $this->fieldConfigStorage->load('paragraph.' . $paragraphs_type_id . '.' . $paragraph_field_name)) {
$summary = $this->t('Using the %field field on a %type paragraph.', [
'%field' => $paragraph_field->label(),
'%type' => $paragraphs_type->label(),
]);
}
}
return $summary . '
' . parent::getSummary();
}
/**
* {@inheritdoc}
*/
protected function prepareValue($delta, array &$values) {
parent::prepareValue($delta, $values);
$paragraph = $this->paragraphStorage->create([
'type' => $this->configuration['paragraphs_type'],
$this->configuration['paragraph_field'] => $values,
]);
$values = ['entity' => $paragraph];
}
}