You are here

public function MenuPositionRuleOrderForm::buildForm in Menu Position 8

Form constructor.

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.

Overrides FormInterface::buildForm

File

src/Form/MenuPositionRuleOrderForm.php, line 79

Class

MenuPositionRuleOrderForm
Class MenuPositionRuleOrderForm.

Namespace

Drupal\menu_position\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Get all the rules.
  $query = $this->entityTypeManager
    ->getStorage('menu_position_rule')
    ->getQuery();
  $results = $query
    ->sort('weight')
    ->execute();
  $rules = $this->entityTypeManager
    ->getStorage('menu_position_rule')
    ->loadMultiple($results);

  // Menu Position rules order (tabledrag).
  $form['#tree'] = TRUE;
  $form['rules'] = [
    '#type' => 'table',
    '#empty' => $this
      ->t('No rules have been created yet.'),
    '#title' => $this
      ->t('Rules processing order'),
    '#header' => [
      $this
        ->t('Rule'),
      $this
        ->t('Affected Menu'),
      $this
        ->t('Enabled'),
      $this
        ->t('Weight'),
      $this
        ->t('Operations'),
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'rules-weight',
      ],
    ],
  ];

  // Display table of rules.
  foreach ($rules as $rule) {

    /* @var \Drupal\menu_position\Entity\MenuPositionRule $rule */

    /* @var \Drupal\menu_position\Plugin\Menu\MenuPositionLink $menu_link */
    $menu_link = $rule
      ->getMenuLinkPlugin();
    $parent = $this->menu_link_manager
      ->createInstance($menu_link
      ->getParent());

    // @todo Because we're in a loop, try to cache this unless the entity
    //   manager handles all that for us. At least only get the storage once?
    $menu = $this->entityTypeManager
      ->getStorage('menu')
      ->load($menu_link
      ->getMenuName());
    $form['rules'][$rule
      ->getId()] = [
      '#attributes' => [
        'class' => [
          'draggable',
        ],
      ],
      'title' => [
        '#markup' => '<strong>' . $rule
          ->getLabel() . '</strong> (' . $this
          ->t('Positioned under: %title', [
          '%title' => $parent
            ->getTitle(),
        ]) . ')',
      ],
      'menu_name' => [
        '#markup' => $menu
          ->label(),
      ],
      'enabled' => [
        '#type' => 'checkbox',
        '#default_value' => $rule
          ->getEnabled(),
      ],
      'weight' => [
        '#type' => 'weight',
        '#title' => $this
          ->t('Weight for @title', [
          '@title' => $rule
            ->getLabel(),
        ]),
        '#title_display' => 'invisible',
        '#default_value' => $rule
          ->getWeight(),
        '#delta' => ceil(count($rules) / 2),
        '#attributes' => [
          'class' => [
            'rules-weight',
          ],
        ],
      ],
      'operations' => [
        '#type' => 'dropbutton',
        '#links' => [
          'edit' => [
            'title' => $this
              ->t('Edit'),
            'url' => Url::fromRoute('entity.menu_position_rule.edit_form', [
              'menu_position_rule' => $rule
                ->getId(),
            ]),
          ],
          'delete' => [
            'title' => $this
              ->t('Delete'),
            'url' => Url::fromRoute('entity.menu_position_rule.delete_form', [
              'menu_position_rule' => $rule
                ->getId(),
            ]),
          ],
        ],
      ],
    ];
  }
  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save configuration'),
    '#button_type' => 'primary',
  ];

  // By default, render the form using theme_system_config_form().
  $form['#theme'] = 'system_config_form';
  return $form;
}