You are here

public function HomeboxSettingsForm::buildBlocksForm in Homebox 8

Build block edit form.

Return value

array Block form array.

Throws

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

1 call to HomeboxSettingsForm::buildBlocksForm()
HomeboxSettingsForm::form in src/Form/HomeboxSettingsForm.php
Gets the actual form array to be built.

File

src/Form/HomeboxSettingsForm.php, line 167

Class

HomeboxSettingsForm
Class HomeboxForm.

Namespace

Drupal\homebox\Form

Code

public function buildBlocksForm() {

  // Build blocks first for each region.
  $blocks = [];

  /* @var \Drupal\homebox\Entity\HomeboxInterface $homebox */
  $homebox = $this->entity;
  $homebox_blocks = $homebox
    ->getBlocks();
  if ($this->request->query
    ->has('block-delete')) {
    $delete = $this->request->query
      ->get('block-delete');
    foreach ($homebox_blocks as $id => $block) {
      if ($block['id'] == $delete) {
        unset($homebox_blocks[$id]);
        break;
      }
    }
    $homebox
      ->setBlocks($homebox_blocks);
    $homebox
      ->save();
  }
  if ($this->request->query
    ->has('block-disable')) {
    $disable = $this->request->query
      ->get('block-disable');
    foreach ($homebox_blocks as $id => $block) {
      if ($block['id'] == $disable) {
        $homebox_blocks[$id]['status'] = FALSE;
        break;
      }
    }
    $homebox
      ->setBlocks($homebox_blocks);
    $homebox
      ->save();
  }
  if ($this->request->query
    ->has('block-enable')) {
    $enable = $this->request->query
      ->get('block-enable');
    foreach ($homebox_blocks as $id => $block) {
      if ($block['id'] == $enable) {
        $homebox_blocks[$id]['status'] = TRUE;
        break;
      }
    }
    $homebox
      ->setBlocks($homebox_blocks);
    $homebox
      ->save();
  }
  foreach ($homebox_blocks as $block) {
    $definition = $this->blockManager
      ->getDefinition($block['id']);
    $blocks[$block['region']][$block['id']] = [
      'label' => $definition['admin_label'],
      'entity_id' => $block['id'],
      'entity' => NULL,
      'category' => $definition['category'],
      'status' => $block['status'],
    ];
    if (isset($block['weight'])) {
      $blocks[$block['region']][$block['id']]['weight'] = $block['weight'];
    }
    if (isset($block['title'])) {
      $blocks[$block['region']][$block['id']]['title_override'] = $block['title'];
    }
  }
  $placement = FALSE;
  if ($this->request->query
    ->has('block-placement')) {
    $placement = $this->request->query
      ->get('block-placement');
    $region = $this->request->query
      ->get('region');
    foreach ($homebox_blocks as $id => $block) {
      if ($block['id'] == $placement) {
        unset($homebox_blocks[$id]);
        break;
      }
    }
    $homebox_blocks[] = [
      'id' => $placement,
      'weight' => 0,
      'region' => $region,
      'status' => TRUE,
    ];
    $homebox
      ->setBlocks($homebox_blocks);
    $homebox
      ->save();
    $form['#attached']['drupalSettings']['blockPlacement'] = $placement;
    $definition = $this->blockManager
      ->getDefinition($placement);
    $blocks[$region][$placement] = [
      'label' => $definition['admin_label'],
      'entity_id' => $placement,
      'weight' => 0,
      'entity' => NULL,
      'category' => $definition['category'],
      'status' => TRUE,
    ];
  }
  $form = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Block'),
      $this
        ->t('Custom title'),
      $this
        ->t('Category'),
      $this
        ->t('Region'),
      $this
        ->t('Weight'),
      $this
        ->t('Operations'),
    ],
    '#attributes' => [
      'id' => 'blocks',
    ],
  ];

  // Weights range from -delta to +delta, so delta should be at least half
  // of the amount of blocks present. This makes sure all blocks in the same
  // region get an unique weight.
  $weight_delta = round(count($blocks) / 2);

  // @todo remove duplicated code
  $layoutInstance = $this->layoutPluginManager
    ->createInstance($homebox
    ->getRegions(), []);

  /** @var \Drupal\Core\Layout\LayoutDefinition $plugin_definition */
  $plugin_definition = $layoutInstance
    ->getPluginDefinition();

  // Loop over each region and build blocks.
  $regions = $plugin_definition
    ->getRegions();
  $region_list = [];
  foreach ($regions as $name => $label) {
    $region_list[$name] = $label['label'];
  }
  foreach ($regions as $region => $title) {
    $form['#tabledrag'][] = [
      'action' => 'match',
      'relationship' => 'sibling',
      'group' => 'block-region-select',
      'subgroup' => 'block-region-' . $region,
      'hidden' => FALSE,
    ];
    $form['#tabledrag'][] = [
      'action' => 'order',
      'relationship' => 'sibling',
      'group' => 'block-weight',
      'subgroup' => 'block-weight-' . $region,
    ];
    $form['region-' . $region] = [
      '#attributes' => [
        'class' => [
          'region-title',
          'region-title-' . $region,
        ],
        'no_striping' => TRUE,
      ],
    ];
    $form['region-' . $region]['title'] = [
      '#theme_wrappers' => [
        'container' => [
          '#attributes' => [
            'class' => 'region-title__action',
          ],
        ],
      ],
      '#prefix' => $region,
      '#type' => 'link',
      '#title' => $this
        ->t('Place block <span class="visually-hidden">in the @region region</span>', [
        '@region' => $title['label'],
      ]),
      '#url' => Url::fromRoute('homebox.block.admin_library', [
        'homebox' => $homebox
          ->id(),
      ], [
        'query' => [
          'region' => $region,
        ],
      ]),
      '#wrapper_attributes' => [
        'colspan' => 5,
      ],
      '#attributes' => [
        'class' => [
          'use-ajax',
          'button',
          'button--small',
        ],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => Json::encode([
          'width' => 700,
        ]),
      ],
    ];
    $form['region-' . $region . '-message'] = [
      '#attributes' => [
        'class' => [
          'region-message',
          'region-' . $region . '-message',
          empty($blocks[$region]) ? 'region-empty' : 'region-populated',
        ],
      ],
    ];
    $form['region-' . $region . '-message']['message'] = [
      '#markup' => '<em>' . $this
        ->t('No blocks in this region') . '</em>',
      '#wrapper_attributes' => [
        'colspan' => 5,
      ],
    ];
    if (isset($blocks[$region])) {
      foreach ($blocks[$region] as $info) {
        $block_id = $info['entity_id'];
        $form[$block_id] = [
          '#attributes' => [
            'class' => [
              'draggable',
            ],
          ],
        ];
        $form[$block_id]['#attributes']['class'][] = $info['status'] ? 'block-enabled' : 'block-disabled';
        if ($placement && $placement == Html::getClass($block_id)) {
          $form[$block_id]['#attributes']['class'][] = 'color-success';
          $form[$block_id]['#attributes']['class'][] = 'js-block-placed';
        }
        $form[$block_id]['info'] = [
          '#plain_text' => $info['status'] ? $info['label'] : $this
            ->t('@label (disabled)', [
            '@label' => $info['label'],
          ]),
          '#wrapper_attributes' => [
            'class' => [
              'block',
            ],
          ],
        ];
        $form[$block_id]['title_override'] = [
          '#type' => 'textfield',
          '#maxlength' => 55,
        ];
        if (isset($info['title_override'])) {
          $form[$block_id]['title_override']['#default_value'] = $info['title_override'];
        }
        $form[$block_id]['type'] = [
          '#markup' => $info['category'],
        ];
        $form[$block_id]['region-theme']['region'] = [
          '#type' => 'select',
          '#default_value' => $region,
          '#required' => TRUE,
          '#title' => $this
            ->t('Region for @block block', [
            '@block' => $info['label'],
          ]),
          '#title_display' => 'invisible',
          '#options' => $region_list,
          '#attributes' => [
            'class' => [
              'block-region-select',
              'block-region-' . $region,
            ],
          ],
          '#parents' => [
            'blocks',
            $block_id,
            'region',
          ],
        ];
        $form[$block_id]['region-theme']['theme'] = [
          '#type' => 'hidden',
          '#value' => $this
            ->getThemeName(),
          '#parents' => [
            'blocks',
            $block_id,
            'theme',
          ],
        ];
        $form[$block_id]['weight'] = [
          '#type' => 'weight',
          '#default_value' => $info['weight'],
          '#delta' => $weight_delta,
          '#title' => $this
            ->t('Weight for @block block', [
            '@block' => $info['label'],
          ]),
          '#title_display' => 'invisible',
          '#attributes' => [
            'class' => [
              'block-weight',
              'block-weight-' . $region,
            ],
          ],
        ];
        $form[$block_id]['operations'] = $this
          ->buildOperations($block_id, $info['status']);
        $form[$block_id]['status'] = [
          '#type' => 'hidden',
          '#value' => strval($info['status']),
          '#title_display' => 'invisible',
          '#attributes' => [
            'class' => [
              'block-status',
              'block-status-' . $region,
            ],
          ],
        ];
      }
    }
  }
  return $form;
}