You are here

public function QuickNodeBlock::blockValidate in Quick Node Block 8

Overrides BlockPluginTrait::blockValidate

File

src/Plugin/Block/QuickNodeBlock.php, line 201

Class

QuickNodeBlock
Provides a Node Block with his display.

Namespace

Drupal\quick_node_block\Plugin\Block

Code

public function blockValidate($form, FormStateInterface $form_state) {

  // Get quick_node value.
  $node_title = $form_state
    ->getValue('quick_node');

  // Get nid.
  preg_match("/.+\\s\\(([^\\)]+)\\)/", $node_title, $matches);
  $nid = $matches[1];

  // Check if the node exists.
  $query = $this->entityTypeManager
    ->getStorage('node')
    ->getQuery();
  $values = $query
    ->condition('nid', $nid)
    ->execute();
  if (empty($values)) {
    $form_state
      ->setErrorByName('quick_node', $this
      ->t('The chosen node does not exist. Choose another one please.'));
  }
  return $form_state;
}