public function EntityEmbedDialog::buildEmbedStep in Entity Embed 8
Form constructor for the entity embedding step.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
1 call to EntityEmbedDialog::buildEmbedStep()
- EntityEmbedDialog::buildForm in src/
Form/ EntityEmbedDialog.php - Form constructor.
File
- src/
Form/ EntityEmbedDialog.php, line 395
Class
- EntityEmbedDialog
- Provides a form to embed entities by specifying data attributes.
Namespace
Drupal\entity_embed\FormCode
public function buildEmbedStep(array $form, FormStateInterface $form_state) {
// Entity element is calculated on every AJAX request/submit.
// See self::buildForm().
$entity_element = $form_state
->get('entity_element');
/** @var \Drupal\embed\EmbedButtonInterface $embed_button */
$embed_button = $form_state
->get('embed_button');
/** @var \Drupal\editor\EditorInterface $editor */
$editor = $form_state
->get('editor');
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $form_state
->get('entity');
$values = $form_state
->getValues();
$form['#title'] = $this
->t('Embed @type', [
'@type' => $entity
->getEntityType()
->getSingularLabel(),
]);
try {
if ($entity
->getEntityType()
->hasLinkTemplate('canonical')) {
$options = [
'attributes' => [
'target' => '_blank',
],
];
$entity_label = $entity
->toLink($entity
->label(), 'canonical', $options)
->toString();
}
elseif ($entity
->getEntityTypeId() == 'file') {
$entity_label = '<a href="' . file_create_url($entity
->getFileUri()) . '" target="_blank">' . $entity
->label() . '</a>';
}
else {
$entity_label = '<a href="' . $entity
->toUrl()
->toString() . '" target="_blank">' . $entity
->label() . '</a>';
}
} catch (\Exception $e) {
$entity_label = $entity
->label();
}
$form['entity'] = [
'#type' => 'item',
'#title' => $this
->t('Selected entity'),
'#markup' => $entity_label,
];
$form['attributes']['data-entity-type'] = [
'#type' => 'hidden',
'#value' => $entity_element['data-entity-type'],
];
$form['attributes']['data-entity-uuid'] = [
'#type' => 'hidden',
'#value' => $entity_element['data-entity-uuid'],
];
if (!empty($entity_element['data-langcode'])) {
$form['attributes']['data-langcode'] = [
'#type' => 'hidden',
'#value' => $entity_element['data-langcode'],
];
}
// Build the list of allowed Entity Embed Display plugins.
$display_plugin_options = $this
->getDisplayPluginOptions($embed_button, $entity);
// If the currently selected display is not in the available options,
// use the first from the list instead. This can happen if an alter
// hook customizes the list based on the entity.
if (!isset($display_plugin_options[$entity_element['data-entity-embed-display']])) {
$entity_element['data-entity-embed-display'] = key($display_plugin_options);
}
// The default Entity Embed Display plugin has been deprecated by the
// rendered entity field formatter.
if ($entity_element['data-entity-embed-display'] === 'default') {
$entity_element['data-entity-embed-display'] = 'entity_reference:entity_reference_entity_view';
}
$form['attributes']['data-entity-embed-display'] = [
'#type' => 'select',
'#title' => $this
->t('Display as'),
'#options' => $display_plugin_options,
'#default_value' => $entity_element['data-entity-embed-display'],
'#required' => TRUE,
'#ajax' => [
'callback' => '::updatePluginConfigurationForm',
'wrapper' => 'data-entity-embed-display-settings-wrapper',
'effect' => 'fade',
],
// Hide the selection if only one option is available.
'#access' => count($display_plugin_options) > 1,
];
$form['attributes']['data-entity-embed-display-settings'] = [
'#type' => 'container',
'#prefix' => '<div id="data-entity-embed-display-settings-wrapper">',
'#suffix' => '</div>',
];
$form['attributes']['data-embed-button'] = [
'#type' => 'value',
'#value' => $embed_button
->id(),
];
$plugin_id = !empty($values['attributes']['data-entity-embed-display']) ? $values['attributes']['data-entity-embed-display'] : $entity_element['data-entity-embed-display'];
if (!empty($plugin_id)) {
if (empty($entity_element['data-entity-embed-display-settings'])) {
$entity_element['data-entity-embed-display-settings'] = [];
}
elseif (is_string($entity_element['data-entity-embed-display-settings'])) {
$entity_element['data-entity-embed-display-settings'] = Json::decode($entity_element['data-entity-embed-display-settings']);
}
$display = $this->entityEmbedDisplayManager
->createInstance($plugin_id, $entity_element['data-entity-embed-display-settings']);
$display
->setContextValue('entity', $entity);
$display
->setAttributes($entity_element);
$form['attributes']['data-entity-embed-display-settings'] += $display
->buildConfigurationForm($form, $form_state);
}
// When Drupal core's filter_align is being used, the text editor may
// offer the ability to change the alignment.
if ($editor
->getFilterFormat()
->filters('filter_align')->status) {
$form['attributes']['data-align'] = [
'#title' => $this
->t('Align'),
'#type' => 'radios',
'#options' => [
'' => $this
->t('None'),
'left' => $this
->t('Left'),
'center' => $this
->t('Center'),
'right' => $this
->t('Right'),
],
'#default_value' => isset($entity_element['data-align']) ? $entity_element['data-align'] : '',
'#wrapper_attributes' => [
'class' => [
'container-inline',
],
],
'#attributes' => [
'class' => [
'container-inline',
],
],
];
}
// When Drupal core's filter_caption is being used, the text editor may
// offer the ability to add a caption.
if ($editor
->getFilterFormat()
->filters('filter_caption')->status) {
$form['attributes']['data-caption'] = [
'#title' => $this
->t('Caption'),
'#type' => 'textarea',
'#rows' => 3,
'#default_value' => isset($entity_element['data-caption']) ? Html::decodeEntities($entity_element['data-caption']) : '',
'#element_validate' => [
'::escapeValue',
],
];
}
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['back'] = [
'#type' => 'submit',
'#value' => $this
->t('Back'),
// No regular submit-handler. This form only works via JavaScript.
'#submit' => [],
'#ajax' => [
'callback' => !empty($this->entityBrowserSettings['display_review']) ? '::submitAndShowReview' : '::submitAndShowSelect',
'event' => 'click',
],
];
$form['actions']['save_modal'] = [
'#type' => 'submit',
'#value' => $this
->t('Embed'),
'#button_type' => 'primary',
// No regular submit-handler. This form only works via JavaScript.
'#submit' => [],
'#ajax' => [
'callback' => '::submitEmbedStep',
'event' => 'click',
],
];
return $form;
}