public function ParagraphsPreviewerWidgetTrait::formElement in Paragraphs Previewer 8
Uses a similar approach to populate a new translation.
See also
\Drupal\content_translation\Controller\ContentTranslationController::prepareTranslation()
File
- src/
Plugin/ Field/ FieldWidget/ ParagraphsPreviewerWidgetTrait.php, line 62
Class
- ParagraphsPreviewerWidgetTrait
- Paragraphs previewer trait to support multiple paragraphs widgets.
Namespace
Drupal\paragraphs_previewer\Plugin\Field\FieldWidgetCode
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$field_name = $this->fieldDefinition
->getName();
$parents = $element['#field_parents'];
$widget_state = static::getWidgetState($parents, $field_name, $form_state);
if (!isset($widget_state['paragraphs'][$delta]['mode']) || !isset($widget_state['paragraphs'][$delta]['entity'])) {
return $element;
}
$item_mode = $widget_state['paragraphs'][$delta]['mode'];
if (!$this
->isPreviewerEnabled($item_mode)) {
return $element;
}
$paragraphs_entity = $widget_state['paragraphs'][$delta]['entity'];
$element_parents = array_merge($parents, [
$field_name,
$delta,
]);
$id_prefix = implode('-', $element_parents);
$previewer_element = [
'#type' => 'submit',
'#value' => t('Preview'),
'#name' => strtr($id_prefix, '-', '_') . '_previewer',
'#weight' => -99999,
'#submit' => [
[
get_class($this),
'submitPreviewerItem',
],
],
'#field_item_parents' => $element_parents,
'#limit_validation_errors' => [
array_merge($parents, [
$field_name,
'add_more',
]),
],
'#delta' => $delta,
'#ajax' => [
'callback' => [
get_class($this),
'ajaxSubmitPreviewerItem',
],
'wrapper' => $widget_state['ajax_wrapper_id'],
'effect' => 'fade',
],
'#access' => $paragraphs_entity
->access('view'),
'#attributes' => [
'class' => [
'link',
'paragraphs-previewer',
],
],
'#attached' => [
'library' => [
'paragraphs_previewer/dialog',
],
],
];
// Set the dialog title.
if (isset($element['top']['paragraph_type_title']['info']['#markup'])) {
$previewer_element['#previewer_dialog_title'] = strip_tags($element['top']['paragraph_type_title']['info']['#markup']);
}
else {
$previewer_element['#previewer_dialog_title'] = t('Preview');
}
if (isset($element['top']['actions']['actions'])) {
// Support "paragraphs" widget.
$element['top']['actions']['actions']['previewer_button'] = $previewer_element;
}
elseif (isset($element['top']['links'])) {
// Support legacy "entity_reference_paragraphs" widget.
$element['top']['links']['previewer_button'] = $previewer_element;
}
else {
// Place in the top as a fallback.
$element['top']['previewer_button'] = $previewer_element;
}
return $element;
}