You are here

public function FloatingMenuBlock::blockForm in Floating Menu 8

Returns the configuration form elements specific to this block plugin.

Blocks that need to add form elements to the normal block configuration form should implement this method.

Parameters

array $form: The form definition array for the block configuration form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The renderable form array representing the entire configuration form.

Overrides BlockPluginTrait::blockForm

File

src/Plugin/Block/FloatingMenuBlock.php, line 71

Class

FloatingMenuBlock
Provides a menu block

Namespace

Drupal\floating_menu\Plugin\Block

Code

public function blockForm($form, FormStateInterface $form_state) {
  $form = parent::blockForm($form, $form_state);
  $config = $this
    ->getConfiguration();
  $form['count_of_items'] = [
    '#type' => 'number',
    '#title' => t('Menu item count'),
    '#description' => t('Select how many items to show in the menu. Save the form and enter details.'),
    '#default_value' => $config['count_of_items'],
  ];
  $form['menu_items'] = [
    '#type' => 'fieldset',
    '#title' => t('Menu Items'),
    '#prefix' => '<div id="menu-item-fieldset-wrapper">',
    '#suffix' => '</div>',
  ];
  $max_items = $config['count_of_items'];
  if (empty($max_items)) {
    $max_items = 5;
  }
  for ($i = 0; $i < $max_items; $i++) {
    $form['menu_items']['menu_item_' . $i] = [
      '#type' => 'fieldset',
      '#title' => t('Menu Item ' . ($i + 1)),
      '#tree' => TRUE,
    ];
    $form['menu_items']['menu_item_' . $i]['menu_item_popup_html'] = [
      '#type' => 'text_format',
      '#format' => 'full_html',
      '#title' => $this
        ->t('Menu Item Popup HTML'),
      '#default_value' => !empty($config['menu_item']) ? $config['menu_item'][$i]['menu_item_popup_html']['value'] : '',
    ];
    $form['menu_items']['menu_item_' . $i]['menu_item_target_url'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Menu Item Target URL'),
      '#default_value' => !empty($config['menu_item']) ? $config['menu_item'][$i]['menu_item_target_url'] : '',
    ];
    $form['menu_items']['menu_item_' . $i]['menu_item_icon'] = [
      '#type' => 'managed_file',
      '#title' => t('Menu Item Icon'),
      '#upload_validators' => array(
        'file_validate_is_image' => array(),
        'file_validate_extensions' => array(
          'jpg jpeg png gif',
        ),
      ),
      '#upload_location' => 'public://',
      '#default_value' => !empty($config['menu_item']) ? [
        $config['menu_item'][$i]['menu_item_icon_file_id'],
      ] : '',
    ];
  }
  return $form;
}