You are here

public function ParagraphEntityForm::entitySelectorForm in Paragraphs Inline Entity Form 8

Same name and namespace in other branches
  1. 2.x src/Plugin/EntityBrowser/Widget/ParagraphEntityForm.php \Drupal\paragraphs_inline_entity_form\Plugin\EntityBrowser\Widget\ParagraphEntityForm::entitySelectorForm()
  2. 2.0.x src/Plugin/EntityBrowser/Widget/ParagraphEntityForm.php \Drupal\paragraphs_inline_entity_form\Plugin\EntityBrowser\Widget\ParagraphEntityForm::entitySelectorForm()
1 call to ParagraphEntityForm::entitySelectorForm()
ParagraphEntityForm::getForm in src/Plugin/EntityBrowser/Widget/ParagraphEntityForm.php
@todo fix the edit form

File

src/Plugin/EntityBrowser/Widget/ParagraphEntityForm.php, line 102

Class

ParagraphEntityForm
A wrapper for EntityForm to provide a two step form where on the first step the user can select the Entity type and on the second step, to create content

Namespace

Drupal\paragraphs_inline_entity_form\Plugin\EntityBrowser\Widget

Code

public function entitySelectorForm(array &$original_form, FormStateInterface $form_state, array $additional_widget_parameters) {
  $path_parts = array_values(array_filter($additional_widget_parameters['path_parts']));
  $form['#prefix'] = '<div id="paragraphs-wysiwyg">';
  $form['#suffix'] = '</div>';
  $form['selected_bundle'] = [
    '#type' => 'hidden',
    '#value' => '',
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['#attached']['library'][] = 'paragraphs_inline_entity_form/dialog';
  if ($path_parts[0] == 'entity-embed') {
    $embed_button = $this->entityTypeManager
      ->getStorage('embed_button')
      ->load($path_parts[3]);
  }
  $allowed_bundles = $embed_button
    ->getTypeSetting('bundles');
  $bundles = $this
    ->getAllowedBundles($allowed_bundles);
  $paragraphs_type_storage = $this->entityTypeManager
    ->getStorage('paragraphs_type');
  $default_icon = drupal_get_path('module', 'paragraphs_inline_entity_form') . '/images/paragraph_thumb.png';
  foreach ($bundles as $bundle => $label) {
    $icon_url = $default_icon;
    if ($paragraphs_type_storage
      ->load($bundle)
      ->getIconFile()) {
      $style = $this->entityTypeManager
        ->getStorage('image_style')
        ->load('thumbnail');
      $path = $paragraphs_type_storage
        ->load($bundle)
        ->getIconFile()
        ->getFileUri();
      $icon_url = $style
        ->buildUrl($path);
    }
    $form['items'][$bundle] = [
      '#type' => 'image_button',
      '#prefix' => '<div class="paragraphs-wysiwyg-add-type">',
      '#suffix' => '<span>' . $label . '</span></div>',
      '#src' => $icon_url,
      '#value' => $label,
      '#attributes' => [
        'data-paragraph-bundle' => $bundle,
      ],
    ];
  }
  return $form;
}