abstract class FieldValidationRuleFormBase in Field Validation 8
Provides a base form for FieldValidationRule.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\field_validation\Form\FieldValidationRuleFormBase
Expanded class hierarchy of FieldValidationRuleFormBase
File
- src/
Form/ FieldValidationRuleFormBase.php, line 17
Namespace
Drupal\field_validation\FormView source
abstract class FieldValidationRuleFormBase extends FormBase {
/**
* The fieldValidationRuleSet.
*
* @var \Drupal\field_validation\FieldValidationRuleSetInterface
*/
protected $fieldValidationRuleSet;
/**
* The fieldValidationRule.
*
* @var \Drupal\field_validation\FieldValidationRuleInterface
*/
protected $fieldValidationRule;
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'field_validation_rule_form';
}
/**
* {@inheritdoc}
*
* @param \Drupal\field_validation\FieldValidationRuleSetInterface $field_validation_rule_set
* The field_validation_rule_set.
* @param string $field_validation_rule
* The field_validation_rule ID.
*
* @return array
* The form structure.
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function buildForm(array $form, FormStateInterface $form_state, FieldValidationRuleSetInterface $field_validation_rule_set = NULL, $field_validation_rule = NULL, $field_name = '') {
$this->fieldValidationRuleSet = $field_validation_rule_set;
try {
$this->fieldValidationRule = $this
->prepareFieldValidationRule($field_validation_rule);
} catch (PluginNotFoundException $e) {
throw new NotFoundHttpException("Invalid field_validation_rule id: '{$field_validation_rule}'.");
}
$request = $this
->getRequest();
if (!$this->fieldValidationRule instanceof ConfigurableFieldValidationRuleInterface) {
throw new NotFoundHttpException();
}
//$form['#attached']['library'][] = 'field_validation/admin';
$form['uuid'] = [
'#type' => 'hidden',
'#value' => $this->fieldValidationRule
->getUuid(),
];
$form['id'] = [
'#type' => 'hidden',
'#value' => $this->fieldValidationRule
->getPluginId(),
];
$form['title'] = [
'#type' => 'textfield',
'#title' => $this
->t('Field Validation Rule title'),
'#default_value' => $this->fieldValidationRule
->getTitle(),
'#required' => TRUE,
];
$entity_type_id = $this->fieldValidationRuleSet
->getAttachedEntityType();
$bundle = $this->fieldValidationRuleSet
->getAttachedBundle();
//$field_options = array();
$field_options = array(
'' => $this
->t('- Select -'),
);
$baseFieldDefinitions = \Drupal::service('entity_field.manager')
->getBaseFieldDefinitions($entity_type_id);
foreach ($baseFieldDefinitions as $base_field_name => $base_field_definition) {
$field_options[$base_field_name] = $base_field_definition
->getLabel();
}
$fieldDefinitions = \Drupal::service('entity_field.manager')
->getFieldDefinitions($entity_type_id, $bundle);
foreach ($fieldDefinitions as $fieldname => $field_definition) {
if (!empty($field_definition
->getTargetBundle())) {
$field_options[$fieldname] = $field_definition
->getLabel();
//if($field_name == 'field_test'){
// $field_all = \Drupal\field\Entity\FieldStorageConfig::loadByName('node', $field_name);
//$field_all = $field_definition->getPropertyDefinitions();
//$field_storage_definitions = \Drupal::entityManager()->getFieldStorageDefinitions('node');
// $field_all = \Drupal\field\Entity\FieldStorageConfig::loadByName('node', $field_name);
// $schema =$field_all->getSchema();
//$field_storage = $field_storage_definitions[$field_name]->getPropertyDefinitions();
//$field_storage = $field_storage_definitions[$field_name]->getPropertyDefinitions();
// drupal_set_message(var_export($schema));
//}
}
}
$default_field_name = $this->fieldValidationRule
->getFieldName();
if (!empty($field_name)) {
$default_field_name = $field_name;
}
$user_input = $form_state
->getUserInput();
$default_field_name = isset($user_input['field_name']) ? $user_input['field_name'] : $default_field_name;
$form['field_name'] = [
'#type' => 'select',
'#title' => $this
->t('Field name'),
'#options' => $field_options,
'#default_value' => $default_field_name,
'#required' => TRUE,
'#ajax' => [
'callback' => [
$this,
'updateColumn',
],
'wrapper' => 'edit-field-name-wrapper',
'event' => 'change',
],
];
//$default_field_name = $form_state->getValue('field_name', $field_name);
$default_column = $this->fieldValidationRule
->getColumn();
//$user_input = $form_state->getUserInput();
$default_column = isset($user_input['column']) ? $user_input['column'] : $default_column;
$column_options = $this
->findColumn($default_field_name);
if (!in_array($default_column, $column_options)) {
$default_column = "";
}
//$default_column = $form_state->getValue('column', $default_column);
//if()
$form['column'] = [
'#type' => 'select',
'#title' => $this
->t('Column of field'),
'#options' => $column_options,
'#default_value' => $default_column,
'#required' => TRUE,
'#prefix' => '<div id="edit-field-name-wrapper">',
'#suffix' => '</div>',
'#validated' => TRUE,
];
$form['data'] = $this->fieldValidationRule
->buildConfigurationForm([], $form_state);
$form['error_message'] = [
'#type' => 'textfield',
'#title' => $this
->t('Error message'),
'#default_value' => $this->fieldValidationRule
->getErrorMessage(),
'#required' => TRUE,
'#maxlength' => 255,
];
$form['data']['#tree'] = TRUE;
//drupal_set_message('term_id:' . var_export($form['data']));
// Check the URL for a weight, then the fieldValidationRule, otherwise use default.
$form['weight'] = [
'#type' => 'hidden',
'#value' => $request->query
->has('weight') ? (int) $request->query
->get('weight') : $this->fieldValidationRule
->getWeight(),
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#button_type' => 'primary',
];
$form['actions']['cancel'] = [
'#type' => 'link',
'#title' => $this
->t('Cancel'),
'#url' => $this->fieldValidationRuleSet
->toUrl('edit-form'),
'#attributes' => [
'class' => [
'button',
],
],
];
return $form;
}
/**
* Handles switching the configuration type selector.
*/
public function updateColumn($form, FormStateInterface $form_state) {
//$form['column']['#default_value'] = '';
//$form['column']['#options'] = $this->findColumn($form_state->getValue('field_name'));
//\Drupal::logger('field_validation')->notice('123:' . $form_state->getValue('field_name'));
// \Drupal::logger('field_validation')->notice('123:' . var_export($form['column']['#options'], true));
/*$form['column']['#options'] = array(
'value' => 'value',
);
*/
// \Drupal::logger('field_validation')->notice('123abc:' . var_export($form['column']['#options'], true));
return $form['column'];
//return $form;
}
/**
* Handles switching the configuration type selector.
*/
protected function findColumn($field_name) {
//\Drupal::logger('field_validation')->notice('1234:' . $field_name);
$column_options = [
'' => $this
->t('- Select -'),
];
if (empty($field_name)) {
return $column_options;
}
$entity_type_id = $this->fieldValidationRuleSet
->getAttachedEntityType();
$baseFieldDefinitions = \Drupal::service('entity_field.manager')
->getBaseFieldDefinitions($entity_type_id);
$schema = [];
if (isset($baseFieldDefinitions[$field_name])) {
$field_info = $baseFieldDefinitions[$field_name];
$schema = $field_info
->getSchema();
}
else {
$field_info = \Drupal\field\Entity\FieldStorageConfig::loadByName($entity_type_id, $field_name);
$schema = $field_info
->getSchema();
}
// \Drupal::logger('field_validation')->notice('1234:' . var_export($schema, true));
if (!empty($schema['columns'])) {
$columns = $schema['columns'];
foreach ($columns as $key => $value) {
$column_options[$key] = $key;
}
}
return $column_options;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// The fieldValidationRule configuration is stored in the 'data' key in the form,
// pass that through for validation.
$data = $form_state
->getValue('data');
if (empty($data)) {
$data = [];
}
$field_validation_rule_data = (new FormState())
->setValues($data);
$this->fieldValidationRule
->validateConfigurationForm($form, $field_validation_rule_data);
// Update the original form values.
$form_state
->setValue('data', $field_validation_rule_data
->getValues());
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state
->cleanValues();
//drupal_flush_all_caches();
// The fieldValidationRule configuration is stored in the 'data' key in the form,
// pass that through for submission.
$field_validation_rule_data = (new FormState())
->setValues($form_state
->getValue('data'));
$this->fieldValidationRule
->submitConfigurationForm($form, $field_validation_rule_data);
// Update the original form values.
$form_state
->setValue('data', $field_validation_rule_data
->getValues());
$this->fieldValidationRule
->setTitle($form_state
->getValue('title'));
$this->fieldValidationRule
->setWeight($form_state
->getValue('weight'));
$this->fieldValidationRule
->setFieldName($form_state
->getValue('field_name'));
$this->fieldValidationRule
->setColumn($form_state
->getValue('column'));
$this->fieldValidationRule
->setErrorMessage($form_state
->getValue('error_message'));
if (!$this->fieldValidationRule
->getUuid()) {
$this->fieldValidationRuleSet
->addFieldValidationRule($this->fieldValidationRule
->getConfiguration());
}
else {
$this->fieldValidationRuleSet
->deleteFieldValidationRule($this->fieldValidationRule);
$this->fieldValidationRuleSet
->addFieldValidationRule($this->fieldValidationRule
->getConfiguration());
}
$this->fieldValidationRuleSet
->save();
$this
->messenger()
->addMessage($this
->t('The rule was successfully applied.'));
$form_state
->setRedirectUrl($this->fieldValidationRuleSet
->toUrl('edit-form'));
}
/**
* Converts a field_validation_rule ID into an object.
*
* @param string $field_validation_rule
* The field_validation_rule ID.
*
* @return \Drupal\field_validation\FieldValidationRuleInterface
* The field_validation_rule object.
*/
protected abstract function prepareFieldValidationRule($field_validation_rule);
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FieldValidationRuleFormBase:: |
protected | property | The fieldValidationRule. | |
FieldValidationRuleFormBase:: |
protected | property | The fieldValidationRuleSet. | |
FieldValidationRuleFormBase:: |
public | function |
Overrides FormInterface:: |
2 |
FieldValidationRuleFormBase:: |
protected | function | Handles switching the configuration type selector. | |
FieldValidationRuleFormBase:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
FieldValidationRuleFormBase:: |
abstract protected | function | Converts a field_validation_rule ID into an object. | 2 |
FieldValidationRuleFormBase:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
FieldValidationRuleFormBase:: |
public | function | Handles switching the configuration type selector. | |
FieldValidationRuleFormBase:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
87 |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |