You are here

public function CheeseburgerMenuTrigger::buildConfigurationForm in Cheeseburger Menu 5.0.x

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides BlockBase::buildConfigurationForm

File

src/Plugin/Block/CheeseburgerMenuTrigger.php, line 103

Class

CheeseburgerMenuTrigger
Provides a 'CheeseburgerMenu' block.

Namespace

Drupal\cheeseburger_menu\Plugin\Block

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildConfigurationForm($form, $form_state);
  $conditions = [
    'plugin' => 'cheeseburger_menu',
  ];
  $form_object = $form_state
    ->getFormObject();
  $theme = $this->configFactory
    ->get('system.theme')
    ->get('default');
  if ($form_object && method_exists($form_object, 'getEntity')) {
    $block = $form_object
      ->getEntity();
    $theme = $block
      ->getTheme();
    $conditions['theme'] = $block
      ->getTheme();
  }
  $block_options = array_map(function ($menu_item) {
    return $menu_item
      ->label() . ' (' . $menu_item
      ->id() . ')';
  }, $this->entityTypeManager
    ->getStorage('block')
    ->loadByProperties($conditions));
  $form['block_to_trigger'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Block to trigger'),
    '#options' => $block_options,
    '#default_value' => $this->configuration['block_to_trigger'],
    '#required' => TRUE,
  ];
  $breakpoints = [];
  if ($this->breakPointManager) {
    foreach ($this->breakPointManager
      ->getGroups() as $group_id => $group) {
      $providers = $this->breakPointManager
        ->getGroupProviders($group_id);
      if (isset($providers[$theme])) {
        foreach ($this->breakPointManager
          ->getBreakpointsByGroup($group_id) as $breakpoint) {
          $breakpoints[$breakpoint
            ->getMediaQuery()] = "{$breakpoint->getLabel()} ({$breakpoint->getMediaQuery()})";
        }
      }
    }
  }
  $form['breakpoints'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Breakpoints'),
    '#descriptions' => $this
      ->t('Choose breakpoints on which you want your trigger to appear'),
    '#options' => $breakpoints,
    '#default_value' => $this->configuration['breakpoints'],
    '#access' => FALSE,
  ];
  $form['custom_media_query'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Custom media query'),
    '#description' => '<ul><li>' . $this
      ->t('Custom media query when trigger will not be displayed. Leave empty to show all the time.') . '</li><li>' . $this
      ->t('Example: @media_query', [
      '@media_query' => 'all and (min-width: 500px)',
    ]) . '</li>',
    '#default_value' => $this->configuration['custom_media_query'],
  ];
  return $form;
}