You are here

public function BlocksSyncForm::buildForm in Structure Sync 8

Same name and namespace in other branches
  1. 2.x src/Form/BlocksSyncForm.php \Drupal\structure_sync\Form\BlocksSyncForm::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

src/Form/BlocksSyncForm.php, line 66

Class

BlocksSyncForm
Import and export form for content in structure, like taxonomy terms.

Namespace

Drupal\structure_sync\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $helper = new StructureSyncHelper();
  $form['title'] = [
    '#type' => 'page_title',
    '#title' => $this
      ->t('Custom blocks'),
  ];
  $form['export'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Export'),
    '#weight' => 1,
    '#open' => TRUE,
  ];
  $form['export']['blocks'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Export custom blocks'),
    '#name' => 'exportBlocks',
    '#button_type' => 'primary',
    '#submit' => [
      [
        $helper,
        'exportCustomBlocks',
      ],
    ],
  ];
  $blockList = [];
  $blocks = $this->entityTypeManager
    ->getStorage('block_content')
    ->loadMultiple();
  foreach ($blocks as $block) {
    $blockList[$block
      ->uuid()] = $block->info
      ->getValue()[0]['value'];
  }
  $form['export']['export_block_list'] = [
    '#type' => 'checkboxes',
    '#options' => $blockList,
    '#default_value' => array_keys($blockList),
    '#title' => $this
      ->t('Select the custom blocks you would like to export'),
  ];
  $form['import'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Import'),
    '#weight' => 2,
    '#open' => TRUE,
  ];
  $block_list = $this
    ->config('structure_sync.data')
    ->get('blocks');
  if (empty($block_list)) {
    $form['import']['import_no_data'] = [
      '#type' => 'markup',
      '#markup' => $this
        ->t("There's no data to import, please do an export first."),
    ];
    return $form;
  }
  $form['import']['import_blocks_safe'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Import custom blocks (safely)'),
    '#name' => 'importBlocksSafe',
    '#button_type' => 'primary',
    '#submit' => [
      [
        $helper,
        'importCustomBlocksSafe',
      ],
    ],
  ];
  $form['import']['import_blocks_full'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Import custom blocks (full)'),
    '#name' => 'importBlocksFull',
    '#submit' => [
      [
        $helper,
        'importCustomBlocksFull',
      ],
    ],
  ];
  $form['import']['import_blocks_force'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Import custom blocks (force)'),
    '#name' => 'importBlocksForce',
    '#submit' => [
      [
        $helper,
        'importCustomBlocksForce',
      ],
    ],
  ];
  $block_list_config = [];
  foreach ($block_list as $block) {
    if ($block['revision_id'] === $block['rev_id_current']) {
      $block_list_config[$block['uuid']] = $block['info'];
    }
  }
  $form['import']['import_block_list'] = [
    '#type' => 'checkboxes',
    '#options' => $block_list_config,
    '#default_value' => array_keys($block_list_config),
    '#title' => $this
      ->t('Select the custom blocks you would like to import'),
  ];
  return $form;
}