You are here

public function CustomSearchBlock::blockValidate in Custom Search 8

Overrides \Drupal\block\BlockBase::blockValidate().

Overrides BlockPluginTrait::blockValidate

File

src/Plugin/Block/CustomSearchBlock.php, line 835

Class

CustomSearchBlock
Provides a 'Custom Search form' block.

Namespace

Drupal\custom_search\Plugin\Block

Code

public function blockValidate($form, FormStateInterface $form_state) {
  if ($form_state
    ->getValue([
    'paths',
    'list',
  ]) != '') {
    $lines = explode("\n", $form_state
      ->getValue([
      'paths',
      'list',
    ]));
    foreach ($lines as $line) {
      if (strpos($line, '|') < 1) {
        $form_state
          ->setErrorByName('list', $this
          ->t('Custom path must be in the form <em>path</em>|<em>label</em>.'));
        break;
      }
    }
  }
  if ($this->moduleHandler
    ->moduleExists('file')) {

    // Handle file uploads.
    $validators = [
      'file_validate_is_image' => [],
    ];

    // Check for a new uploaded logo.
    $file = file_save_upload('settings', $validators, FALSE, 0);
    if (isset($file)) {

      // File upload was attempted.
      if ($file) {
        $directory_path = 'public://custom_search';
        \Drupal::service('file_system')
          ->prepareDirectory($directory_path, FileSystemInterface::CREATE_DIRECTORY);
        \Drupal::service('file_system')
          ->copy($file
          ->getFileUri(), $directory_path);
        $form_state
          ->setValue([
          'submit',
          'image_path',
        ], $filename);
      }
      else {

        // File upload failed.
        $form_state
          ->setErrorByName('image', $this
          ->t('The submit image could not be uploaded.'));
      }
    }

    // If the user provided a path for a logo or favicon file, make sure a
    // file exists at that path.
    if (!$form_state
      ->isValueEmpty([
      'submit',
      'image_path',
    ])) {
      $path = $this
        ->validatePath($form_state
        ->getValue([
        'submit',
        'image_path',
      ]));
      if (!$path) {
        $form_state
          ->setErrorByName('image_path', $this
          ->t('The submit image path is invalid.'));
      }
    }
  }
}