You are here

public function BlockManagerForm::buildForm in Gutenberg 8

Same name and namespace in other branches
  1. 8.2 modules/gutenberg_cloud/src/Form/BlockManagerForm.php \Drupal\gutenberg_cloud\Form\BlockManagerForm::buildForm()

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 ConfigFormBase::buildForm

File

modules/gutenberg_cloud/src/Form/BlockManagerForm.php, line 88

Class

BlockManagerForm
Configure Gutenberg Cloud blocks.

Namespace

Drupal\gutenberg_cloud\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $cloudBlocks = $this->blockManager
    ->loadAllFromRemote();
  if (!empty($cloudBlocks)) {
    $this
      ->addFilters($form);
    $form['blocks'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'gutenberg-cloud__blocks',
        ],
      ],
    ];

    // Loop index - default order of the blocks.
    $weight = 0;
    foreach ($cloudBlocks as $key => $block) {
      $id = Html::cleanCssIdentifier($block
        ->getName());
      $form['blocks'][$key] = [
        '#type' => 'fieldset',
        '#open' => TRUE,
        '#id' => $id,
        '#attributes' => [
          'class' => [
            'gutenberg-cloud__item',
          ],
          'data-popularity' => $block
            ->getRaw()->installs,
          'data-weight' => $weight++,
        ],
      ];
      $form['blocks'][$key]['details_wrapper'] = [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'gutenberg-cloud__details',
          ],
        ],
      ];
      $form['blocks'][$key]['details_wrapper']['details'] = [
        '#type' => 'button',
        '#value' => $this
          ->t('More details'),
        '#ajax' => [
          'callback' => '::viewDetails',
          'event' => 'click',
        ],
        '#attributes' => [
          'class' => [
            'details',
          ],
        ],
        '#name' => 'details__' . $block
          ->getName(),
      ];
      $form['blocks'][$key]['details_wrapper']['image'] = [
        '#theme' => 'image',
        '#uri' => $block
          ->getAssetUrl('screenshot', $this->blockManager
          ->getCdnUrl()),
      ];
      $form['blocks'][$key]['title'] = [
        '#type' => 'html_tag',
        '#tag' => 'h2',
        '#value' => $block
          ->getLabel(),
        '#attributes' => [
          'class' => [
            'title',
          ],
        ],
      ];
      $form['blocks'][$key]['install'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Install'),
        '#ajax' => [
          'callback' => '::installCallback',
          'event' => 'click',
          'method' => 'replace',
          'effect' => 'fade',
          'wrapper' => $id,
          'progress' => [
            'type' => 'submit',
            'message' => $this
              ->t('Installing...'),
          ],
        ],
        '#button_type' => 'primary',
        '#name' => 'install__' . $block
          ->getName(),
        '#access' => FALSE,
      ];
      $form['blocks'][$key]['update'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Update'),
        '#ajax' => [
          'callback' => '::updateCallback',
          'event' => 'click',
          'method' => 'replace',
          'effect' => 'fade',
          'wrapper' => $id,
          'progress' => [
            'type' => 'submit',
            'message' => $this
              ->t('Updating...'),
          ],
        ],
        '#button_type' => 'primary',
        '#name' => 'update__' . $block
          ->getName(),
        '#access' => FALSE,
      ];
      $form['blocks'][$key]['uninstall'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Uninstall'),
        '#ajax' => [
          'callback' => '::uninstallCallback',
          'event' => 'click',
          'method' => 'replace',
          'effect' => 'fade',
          'wrapper' => $id,
          'progress' => [
            'type' => 'throbber',
            'message' => $this
              ->t('Uninstalling...'),
          ],
        ],
        '#name' => 'uninstall__' . $block
          ->getName(),
        '#access' => FALSE,
      ];
      if (!$this->blockManager
        ->isBlockEnabled($block)) {
        $form['blocks'][$key]['install']['#access'] = TRUE;
      }
      else {
        if ($this->blockManager
          ->hasUpdates($block)) {
          $form['blocks'][$key]['update']['#access'] = TRUE;
        }
        $form['blocks'][$key]['uninstall']['#access'] = TRUE;
        $form['blocks'][$key]['#attributes']['class'][] = 'enabled';
      }
    }
    $form['#attached']['library'] = [
      'gutenberg_cloud/blocks',
    ];
  }
  $form_state
    ->setCached(FALSE);
  return $form;
}