Commit bb07e98c authored by renatog's avatar renatog Committed by renatog
Browse files

Issue #3270180 by RenatoG: Implement a form validate to validate the attributes

parent db4068d5
Loading
Loading
Loading
Loading
+9 −0
Changes for block_class.module: 9 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -71,6 +71,15 @@ function block_class_form_block_form_alter(&$form, FormStateInterface $form_stat

}

/**
 * Form validate.
 */
function block_class_form_block_form_validate(&$form, FormStateInterface $form_state) {

  \Drupal::service('block_class.helper')->blockClassFormValidate($form, $form_state);

}

/**
 * Implements hook_preprocess_HOOK().
 */
+35 −0
Changes for src/Service/BlockClassHelperService.php: 35 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -350,6 +350,41 @@ class BlockClassHelperService {
        $form['third_party_settings']['block_class']['classes']['#autocomplete_route_name'] = 'block_class.autocomplete';
      }
    }

    $form['#validate'][] = 'block_class_form_block_form_validate';
  }

  /**
   * Method to do a form validate.
   */
  public function blockClassFormValidate(&$form, &$form_state) {

    // Get config object.
    $config = \Drupal::config('block_class.settings');

    if (empty($config->get('enable_attributes'))) {
      return;
    }

    if (empty($form_state->getValue('id'))) {
      return;
    }

    $third_party_settings = $form_state->getValue('third_party_settings');

    if (empty($third_party_settings['block_class']['attributes'])) {
      return;
    }

    $attributes = $third_party_settings['block_class']['attributes'];

    $pipe_found = strpos($attributes, '|');

    if ($pipe_found === FALSE) {

      $form_state->setErrorByName('third_party_settings[block_class][attributes]', $this->t("You need to insert a pipe on this field"));
    }

  }

}