You are here

public function Breadcrumb::validateConfigurationForm in Context Breadcrumb 8

File

src/Plugin/ContextReaction/Breadcrumb.php, line 103

Class

Breadcrumb
Provides a content reaction that adds breadcrumb to page.

Namespace

Drupal\context_breadcrumb\Plugin\ContextReaction

Code

public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  parent::validateConfigurationForm($form, $form_state);
  foreach ($form_state
    ->getValue('breadcrumbs') as $i => $breadcrumb) {
    if (!empty($breadcrumb['title']) && empty($breadcrumb['url'])) {
      $form_state
        ->setErrorByName('breadcrumbs][' . $i . '][url', $this
        ->t('@name field is required.', [
        '@name' => 'Url',
      ]));
    }
    if (empty($breadcrumb['title']) && !empty($breadcrumb['url'])) {
      $form_state
        ->setErrorByName('breadcrumbs][' . $i . '][token', $this
        ->t('@name field is required.', [
        '@name' => 'Token',
      ]));
    }
    if (!empty($breadcrumb['title']) && !empty($breadcrumb['url'])) {
      if (ContextBreadcrumbBuilder::isToken($breadcrumb['url']) && empty($breadcrumb['token'])) {
        $form_state
          ->setErrorByName('breadcrumbs][' . $i . '][token', $this
          ->t('The url using token, please select token option.'));
      }
      if (ContextBreadcrumbBuilder::isToken($breadcrumb['title']) && empty($breadcrumb['token'])) {
        $form_state
          ->setErrorByName('breadcrumbs][' . $i . '][token', $this
          ->t('The title using token, please select token option.'));
      }
      if (!ContextBreadcrumbBuilder::isToken($breadcrumb['url']) && !in_array($breadcrumb['url'], [
        '<front>',
        '<nolink>',
      ]) && strpos($breadcrumb['url'], 'http://') === FALSE && strpos($breadcrumb['url'], 'https://') === FALSE && $breadcrumb['url'][0] !== '/') {
        $form_state
          ->setErrorByName('breadcrumbs][' . $i . '][url', $this
          ->t('The url path has to start with a slash.'));
      }
    }
  }
}