You are here

public static function HomeboxPageForm::saveUserLayoutData in Homebox 8

Save user settings.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

\Symfony\Component\HttpFoundation\Request $request: Current request.

Return value

array The form structure.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

1 call to HomeboxPageForm::saveUserLayoutData()
HomeboxPageForm::addBlock in src/Form/HomeboxPageForm.php
Ajax callback to add block.

File

src/Form/HomeboxPageForm.php, line 90

Class

HomeboxPageForm
Class HomeboxPageForm.

Namespace

Drupal\homebox\Form

Code

public static function saveUserLayoutData(array $form, FormStateInterface &$form_state, Request $request) {
  $blocks = $form_state
    ->getValue('blocks');
  if (!is_array($blocks)) {
    parse_str($form_state
      ->getValue('blocks'), $blocks_value);
    $blocks = [];
    foreach ($blocks_value as $value) {
      parse_str($value, $block);

      // @todo Check block deleting in js. Possible there's parent element not remove.
      if ($block['id'] && $block['status']) {
        $blocks[] = $block;
      }
    }
  }
  $user = \Drupal::currentUser();
  $homebox = $request->attributes
    ->get('homebox');
  $homebox_layout_storage = \Drupal::entityTypeManager()
    ->getStorage('homebox_layout');
  $homebox_layout_id = $homebox_layout_storage
    ->getQuery()
    ->condition('type', $homebox
    ->id())
    ->condition('user_id', $user
    ->id())
    ->execute();
  if (!empty($homebox_layout_id)) {
    $homebox_layout = $homebox_layout_storage
      ->load(array_shift($homebox_layout_id));
  }
  else {

    // Create homebox layout entity for current user.
    $values = [
      'user_id' => $user
        ->id(),
      'name' => $homebox
        ->id(),
      'status' => 1,
      'type' => $homebox
        ->id(),
    ];
    $homebox_layout = HomeboxLayout::create($values);
  }

  // Save user settings.
  // @todo: use dependency injection!!!
  $serializer = \Drupal::service('serializer');
  $data = $serializer
    ->serialize($blocks, 'json');
  $homebox_layout
    ->set('settings', $data);
  $homebox_layout
    ->set('layout_id', $homebox
    ->getRegions());
  $homebox_layout
    ->save();
  return $form;
}