You are here

protected function BlockManagerForm::performCallbackAction 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::performCallbackAction()

Save configs and alter the form on ajax callback.

Parameters

array $form: Form structure.

\Drupal\Core\Form\FormStateInterface $form_state: Form state.

string $type: Action to perform.

Return value

array Form element

3 calls to BlockManagerForm::performCallbackAction()
BlockManagerForm::installCallback in modules/gutenberg_cloud/src/Form/BlockManagerForm.php
Block installation callback.
BlockManagerForm::uninstallCallback in modules/gutenberg_cloud/src/Form/BlockManagerForm.php
Block uninstall callback.
BlockManagerForm::updateCallback in modules/gutenberg_cloud/src/Form/BlockManagerForm.php
Block update callback.

File

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

Class

BlockManagerForm
Configure Gutenberg Cloud blocks.

Namespace

Drupal\gutenberg_cloud\Form

Code

protected function performCallbackAction(array &$form, FormStateInterface $form_state, $type = '') {
  $config = $this
    ->config('gutenberg_cloud.blocks');
  $element = $form_state
    ->getTriggeringElement();
  $parents = $element['#array_parents'];
  $output = $form[$parents[0]][$parents[1]];
  $name = str_replace($type . '__', '', $element['#name']);
  switch ($type) {
    case 'update':
    case 'install':
      $block = $this->blockManager
        ->loadRemote($name);
      if ($block instanceof CloudBlockInterface) {
        $config
          ->set($name, $block
          ->getConfig())
          ->save();
      }
      $output['install']['#access'] = FALSE;
      $output['update']['#access'] = FALSE;
      $output['uninstall']['#access'] = TRUE;
      $output['#attributes']['class'][] = 'enabled';
      $message = $type == 'install' ? 'Installed' : 'Updated';
      $this->messenger
        ->addStatus($this
        ->t('@message', [
        '@message' => $message,
      ]));
      break;
    case 'uninstall':
      $config
        ->clear($name)
        ->save();
      $output['install']['#access'] = TRUE;
      $output['update']['#access'] = FALSE;
      $output['uninstall']['#access'] = FALSE;
      $classes = $output['#attributes']['class'];
      $output['#attributes']['class'] = array_diff($classes, [
        'enabled',
      ]);
      $this->messenger
        ->addStatus($this
        ->t('Uninstalled'));
      break;
    default:
      break;
  }
  $this->libraryDiscovery
    ->clearCachedDefinitions();
  return $output;
}