You are here

public function HomeboxPageForm::buildForm in Homebox 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/HomeboxPageForm.php, line 248

Class

HomeboxPageForm
Class HomeboxPageForm.

Namespace

Drupal\homebox\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, HomeboxInterface $homebox = NULL) {
  $is_ajax = $this
    ->getRequest()
    ->isXmlHttpRequest();
  if (!$is_ajax) {
    $session = \Drupal::request()
      ->getSession();
    $session
      ->set('block_buttons_visible', FALSE);
  }

  // Get user layout settings or default homebox settings if not exists.

  /** @var \Drupal\homebox\Entity\HomeboxLayoutInterface $homebox_layout */
  $homebox_layout = $this
    ->getLayoutStorageData($homebox
    ->id(), $this->account
    ->id());
  $blocks_available_for_adding = $homebox
    ->getBlocks();

  // Check if user layout settings exists and haven't been changed.
  if (isset($homebox_layout) && $homebox
    ->getRegions() == $homebox_layout
    ->get('layout_id')
    ->getValue()[0]['value']) {
    $blocks = $homebox_layout
      ->get('settings')
      ->getValue();
    $blocks = $this->serializer
      ->decode(array_shift($blocks)['value'], 'json');
  }
  else {
    $blocks = $blocks_available_for_adding;
  }

  // Get layout regions.

  /** @var \Drupal\Core\Layout\LayoutInterface $layout_instance */
  $layout_instance = \Drupal::service('plugin.manager.core.layout')
    ->createInstance($homebox
    ->getRegions(), []);
  $render = $regions = [];
  $form['#attached']['library'][] = 'homebox/draggable-blocks';
  $form['blocks'] = [
    '#type' => 'hidden',
  ];
  $form['add_block_button'] = [
    '#type' => 'button',
    '#value' => $this
      ->t('Add a block'),
    '#ajax' => [
      'callback' => 'Drupal\\homebox\\Form\\HomeboxPageForm::hideBlockButtons',
      'event' => 'click',
    ],
  ];
  $form['save_form'] = [
    '#type' => 'button',
    '#value' => $this
      ->t('Save'),
    '#attributes' => [
      'class' => [
        'homebox-save-form',
      ],
    ],
    '#ajax' => [
      'callback' => 'Drupal\\homebox\\Form\\HomeboxPageForm::saveUserLayoutData',
      'event' => 'click',
    ],
  ];
  $form['block_buttons'] = [
    '#type' => 'fieldset',
    '#attributes' => [
      'class' => [
        'block_buttons',
      ],
      'style' => 'display: none;',
    ],
  ];

  // Create constant list of blocks available for adding on homebox page.
  foreach ($blocks_available_for_adding as $block) {

    /** @var \Drupal\Core\Block\BlockPluginInterface $block_instance */
    $block_instance = \Drupal::service('plugin.manager.block')
      ->createInstance($block['id']);
    $form['block_buttons']['add_block_' . $block['id']] = [
      '#type' => 'button',
      '#value' => isset($block['title']) && $block['title'] != '' ? $block['title'] : $block_instance
        ->label(),
      '#attributes' => [
        'block_id' => $block['id'],
      ],
      '#ajax' => [
        'callback' => 'Drupal\\homebox\\Form\\HomeboxPageForm::addBlock',
        'wrapper' => 'homebox',
        'event' => 'click',
      ],
    ];
  }
  foreach ($blocks as $block) {
    $block_instance = \Drupal::service('plugin.manager.block')
      ->createInstance($block['id']);
    if ($block['status']) {
      $token = \Drupal::csrfToken()
        ->get((new Random())
        ->string(32));
      $render[$block['id']] = [
        '#type' => 'container',
        '#attributes' => [
          'id' => $token,
        ],
      ];
      $render[$block['id']]['render'] = $block_instance
        ->build();
      $render[$block['id']]['render']['#theme'] = 'homebox_block';
      $render[$block['id']]['render']['#block_id'] = $block_instance
        ->getPluginId();
      $render[$block['id']]['render']['#block_title'] = isset($block['title']) && $block['title'] != '' ? $block['title'] : $block_instance
        ->label();
      $render[$block['id']]['render']['#block_content'] = $block_instance
        ->build();
      $regions[$block['region']][] = $render[$block['id']];
      $regions[$block['region']]['#attributes'] = [
        'id' => 'region_' . $block['region'],
        'class' => 'homebox-column ui-sortable',
      ];
    }
  }
  $plugin_definition = $layout_instance
    ->getPluginDefinition();
  foreach ($plugin_definition
    ->getRegions() as $id => $region) {
    if (!isset($regions[$id])) {
      $regions[$id] = [
        '#markup' => '',
        '#attributes' => [
          'id' => 'region_' . $id,
          'class' => 'homebox-column ui-sortable',
        ],
      ];
    }
  }
  $form['regions'] = $layout_instance
    ->build($regions);
  $form['regions']['#attributes'] = [
    'id' => 'homebox',
  ];
  return $form;
}