public function ConditionalFieldEditForm::buildForm in Conditional Fields 4.x
Same name and namespace in other branches
- 8 src/Form/ConditionalFieldEditForm.php \Drupal\conditional_fields\Form\ConditionalFieldEditForm::buildForm()
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides FormInterface::buildForm
File
- src/
Form/ ConditionalFieldEditForm.php, line 85
Class
- ConditionalFieldEditForm
- An edit form for conditional fields.
Namespace
Drupal\conditional_fields\FormCode
public function buildForm(array $form, FormStateInterface $form_state, $entity_type = NULL, $bundle = NULL, $field_name = NULL, $uuid = NULL) {
if (empty($entity_type) || empty($bundle) || empty($field_name) || empty($uuid)) {
return $form;
}
$form_display_entity = $this->entityTypeManager
->getStorage('entity_form_display')
->load("{$entity_type}.{$bundle}.default");
if (!$form_display_entity) {
return $form;
}
// Retrieve first field from the list.
$field = count(explode('-', $field_name)) > 0 ? explode('-', $field_name)[0] : $field_name;
$field = $form_display_entity
->getComponent($field);
if (empty($field['third_party_settings']['conditional_fields'][$uuid])) {
return $form;
}
$condition = $field['third_party_settings']['conditional_fields'][$uuid];
$settings = $condition['settings'];
// @todo It's not label, but machine_name.
$label = $condition['dependee'];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save settings'),
'#weight' => 50,
];
// Save parameters for submit saving.
$form['entity_type'] = [
'#type' => 'textfield',
'#value' => $entity_type,
'#access' => FALSE,
];
$form['bundle'] = [
'#type' => 'textfield',
'#value' => $bundle,
'#access' => FALSE,
];
$form['field_name'] = [
'#type' => 'textfield',
'#value' => $field_name,
'#access' => FALSE,
];
$form['uuid'] = [
'#type' => 'textfield',
'#value' => $uuid,
'#access' => FALSE,
];
$form['condition'] = [
'#type' => 'select',
'#title' => $this
->t('Condition'),
'#description' => $this
->t('The condition that should be met by the control field %field to trigger the dependency.', [
'%field' => $label,
]),
'#options' => $this->list
->conditionalFieldsConditions(),
'#default_value' => array_key_exists('condition', $settings) ? $settings['condition'] : '',
'#required' => TRUE,
];
$form['values_set'] = [
'#type' => 'select',
'#title' => $this
->t('Values input mode'),
'#description' => $this
->t('The input mode of the values that trigger the dependency.'),
'#options' => [
ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET => $this
->t('Insert value from widget...'),
ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX => $this
->t('Regular expression...'),
'Set of values' => [
ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND => $this
->t('All these values (AND)...'),
ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR => $this
->t('Any of these values (OR)...'),
ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR => $this
->t('Only one of these values (XOR)...'),
ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT => $this
->t('None of these values (NOT)...'),
],
],
'#default_value' => array_key_exists('values_set', $settings) ? $settings['values_set'] : 0,
'#required' => TRUE,
'#states' => [
'visible' => [
':input[name="condition"]' => [
'value' => 'value',
],
],
],
];
$dummy_field = $this
->getDummyField($entity_type, $bundle, $condition, $form_state, $settings['value_form']);
$form['value'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Insert value from widget'),
'#description' => $this
->t('The dependency is triggered when the field has exactly the same value(s) inserted in the widget below.'),
'#states' => [
'visible' => [
':input[name="values_set"]' => [
'value' => (string) ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET,
],
':input[name="condition"]' => [
'value' => 'value',
],
],
'required' => [
':input[name="values_set"]' => [
'value' => (string) ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET,
],
':input[name="condition"]' => [
'value' => 'value',
],
],
],
'#tree' => FALSE,
'field' => $dummy_field,
];
$form['values'] = [
'#type' => 'textarea',
'#title' => $this
->t('Set of values'),
'#description' => $this
->t('The values of the dependee %field that trigger the dependency.', [
'%field' => $label,
]) . '<br>' . $this
->t('Enter one value per line. Note: if the dependee has allowed values, these are actually the keys, not the labels, of those values.'),
'#states' => [
'visible' => [
':input[name="values_set"]' => [
[
'value' => (string) ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND,
],
[
'value' => (string) ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR,
],
[
'value' => (string) ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR,
],
[
'value' => (string) ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT,
],
],
':input[name="condition"]' => [
'value' => 'value',
],
],
'required' => [
':input[name="values_set"]' => [
[
'value' => (string) ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND,
],
[
'value' => (string) ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR,
],
[
'value' => (string) ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR,
],
[
'value' => (string) ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT,
],
],
':input[name="condition"]' => [
'value' => 'value',
],
],
],
];
if (!empty($settings['values']) && is_array($settings['values'])) {
$form['values']['#default_value'] = implode("\n", $settings['values']);
}
elseif (!empty($settings['values']) && is_string($settings['values'])) {
$form['values']['#default_value'] = $settings['values'];
}
else {
$form['values']['#default_value'] = '';
}
$form['regex'] = [
'#type' => 'textfield',
'#title' => $this
->t('Regular expression'),
'#description' => $this
->t('The dependency is triggered when all the values of the dependee %field match the regular expression. The expression should be valid both in PHP and in Javascript. Do not include delimiters.', [
'%field' => $label,
]) . '<br>' . $this
->t('Note: If the dependee has allowed values, these are actually the keys, not the labels, of those values.'),
'#maxlength' => 2048,
'#size' => 120,
'#default_value' => isset($settings['regex']) ? $settings['regex'] : '',
'#states' => [
'visible' => [
':input[name="values_set"]' => [
'value' => (string) ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX,
],
':input[name="condition"]' => [
'value' => 'value',
],
],
'required' => [
':input[name="values_set"]' => [
'value' => (string) ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX,
],
':input[name="condition"]' => [
'value' => 'value',
],
],
],
];
$form['grouping'] = [
'#type' => 'radios',
'#title' => $this
->t('Interaction with other dependencies'),
'#description' => $this
->t('When this target field has more than one control field, how should this condition be evaluated against the others?') . '<br />' . $this
->t('Note that sets will be grouped this way: (ANDs) AND (ORs) AND (XORs).'),
'#options' => [
'AND' => 'AND',
'OR' => 'OR',
'XOR' => 'XOR',
],
'#default_value' => array_key_exists('grouping', $settings) ? $settings['grouping'] : 'AND',
'#required' => TRUE,
];
if ($this
->fieldSupportsInheritance($entity_type, $bundle, $field_name)) {
$form['inheritance'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Inheritance'),
'#description' => $this
->t('This element contains other fields. Apply the settings in this form to the contained fields instead of this one.'),
'#options' => [
'propagate' => $this
->t('Propagate settings to fields contained within this one.'),
'apply_to_parent' => $this
->t('Apply these settings to the this (parent) field also. Requires the "Propagate" setting, above.'),
'recurse' => $this
->t('Apply these settings to group fields contained within this one. Requires the "Propagate" setting, above.'),
],
'#default_value' => array_key_exists('inheritance', $settings) ? $settings['inheritance'] : [],
];
$form['inheritance']['apply_to_parent'] = [
'#states' => [
'disabled' => [
':input[name="inheritance[propagate]"]' => [
'checked' => FALSE,
],
],
],
];
$form['inheritance']['recurse']['#states'] = $form['inheritance']['apply_to_parent']['#states'];
}
$form['entity_edit'] = [
'#type' => 'details',
'#title' => $this
->t('Edit context settings'),
'#description' => $this
->t('These settings apply when the @entity is being added or edited in a form.', [
'@entity' => $label,
]),
'#open' => TRUE,
];
$form['entity_edit'] += $this
->buildEditContextSettings([], $form_state, $condition);
return $form;
}