public function EntityEmbedDialog::buildSelectStep in Entity Embed 8
Form constructor for the entity selection 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::buildSelectStep()
- EntityEmbedDialog::buildForm in src/
Form/ EntityEmbedDialog.php - Form constructor.
File
- src/
Form/ EntityEmbedDialog.php, line 241
Class
- EntityEmbedDialog
- Provides a form to embed entities by specifying data attributes.
Namespace
Drupal\entity_embed\FormCode
public function buildSelectStep(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');
$entity = $form_state
->get('entity');
$form['attributes']['data-entity-type'] = [
'#type' => 'value',
'#value' => $entity_element['data-entity-type'],
];
$label = $this
->t('Label');
// Attempt to display a better label if we can by getting it from
// the label field definition.
$entity_type = $this->entityTypeManager
->getDefinition($entity_element['data-entity-type']);
if ($entity_type
->entityClassImplements(FieldableEntityInterface::class) && $entity_type
->hasKey('label')) {
$field_definitions = $this->entityFieldManager
->getBaseFieldDefinitions($entity_type
->id());
if (isset($field_definitions[$entity_type
->getKey('label')])) {
$label = $field_definitions[$entity_type
->getKey('label')]
->getLabel();
}
}
$form['#title'] = $this
->t('Select @type to embed', [
'@type' => $entity_type
->getSingularLabel(),
]);
if ($this->entityBrowser) {
$this->eventDispatcher
->addListener(Events::REGISTER_JS_CALLBACKS, [
$this,
'registerJSCallback',
]);
$form['entity_browser'] = [
'#type' => 'entity_browser',
'#entity_browser' => $this->entityBrowser
->id(),
'#cardinality' => 1,
'#entity_browser_validators' => [
'entity_type' => [
'type' => $entity_element['data-entity-type'],
],
],
];
}
else {
$form['entity_id'] = [
'#type' => 'entity_autocomplete',
'#target_type' => $entity_element['data-entity-type'],
'#title' => $label,
'#default_value' => $entity,
'#required' => TRUE,
'#description' => $this
->t('Type label and pick the right one from suggestions. Note that the unique ID will be saved.'),
'#maxlength' => 255,
];
if ($bundles = $embed_button
->getTypeSetting('bundles')) {
$form['entity_id']['#selection_settings']['target_bundles'] = $bundles;
}
}
if (!empty($entity_element['data-langcode'])) {
$form['attributes']['data-langcode'] = [
'#type' => 'hidden',
'#value' => $entity_element['data-langcode'],
];
}
$form['attributes']['data-entity-uuid'] = [
'#type' => 'value',
'#title' => $entity_element['data-entity-uuid'],
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['save_modal'] = [
'#type' => 'submit',
'#value' => $this
->t('Next'),
'#button_type' => 'primary',
// No regular submit-handler. This form only works via JavaScript.
'#submit' => [],
'#ajax' => [
'callback' => '::submitSelectStep',
'event' => 'click',
],
'#attributes' => [
'class' => [
'js-button-next',
],
],
];
return $form;
}