class EditFieldForm in Form Defaults 8
@class EditFieldForm
Form to edit the field title and description.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Formdefaults\Form\EditFieldForm
Expanded class hierarchy of EditFieldForm
File
- src/
Form/ EditFieldForm.php, line 14
Namespace
Drupal\Formdefaults\FormView source
class EditFieldForm extends FormBase {
public function getFormId() {
return 'formdefaults_edit_field';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form_array = $_SESSION['formdefaults_forms'];
$path_args = explode('/', current_path());
$formid = $path_args[1];
$fieldname = $path_args[2];
$originalfields = @$form_array[$formid][$fieldname] ? $form_array[$formid][$fieldname] : array();
$savedform = formdefaults_getform($formid);
$weight_range = range(-50, 50);
$weights = array(
'unset' => 'unset',
);
foreach ($weight_range as $weight) {
$weights[(string) $weight] = (string) $weight;
}
if (is_array(@$savedform[$fieldname])) {
$formfields = array_merge($originalfields, @$savedform[$fieldname]);
}
else {
$formfields = $originalfields;
}
$type = $formfields['type'];
if (!$type) {
if (isset($formfields['input_format'])) {
$type = 'markup';
}
}
if (@$originalfields['type']) {
$type = $originalfields['type'];
}
$form['formid'] = array(
'#type' => 'value',
'#value' => $formid,
);
$form['fieldname'] = array(
'#type' => 'value',
'#value' => $fieldname,
);
$form['type'] = array(
'#type' => 'value',
'#title' => 'Field Type',
'#value' => $type,
);
$form['warning'] = array(
'#type' => 'markup',
'#value' => 'Some text to edit',
);
$form['hide_it'] = array(
'#type' => 'checkbox',
'#title' => 'Hide this field',
'#description' => 'Checking this box will convert the field to a hidden field.' . ' You will need to use the edit form link to unhide them.',
'#default_value' => @$formfields['hide_it'],
);
if ($type == 'markup') {
$form['value'] = array(
'#type' => 'text_format',
'#title' => 'Text or markup',
'#rows' => 30,
'#cols' => 80,
'#format' => @$formfields['input_format'],
'#default_value' => @$formfields['value'],
);
$form['value_original'] = array(
'#type' => 'item',
'#title' => 'Original value',
'#value' => @$originalfields['value'],
);
}
else {
$form['title'] = array(
'#type' => 'textfield',
'#title' => 'Field Title',
'#default_value' => @$formfields['title'],
);
$form['title_old'] = array(
'#type' => 'item',
'#title' => 'Original Title',
'#value' => @$originalfields['title'],
);
$form['description'] = array(
'#type' => 'textarea',
'#title' => 'Field Description',
'#default_value' => $formfields['description'],
'#rows' => 30,
'#cols' => 80,
);
$form['description_old'] = array(
'#type' => 'item',
'#title' => 'Original Description',
'#value' => $originalfields['description'],
);
}
if ($type == 'fieldset') {
$truefalse = array(
'' => 'Leave alone',
TRUE => 'Yes',
FALSE => 'No',
);
$form['collapsible'] = array(
'#type' => 'radios',
'#title' => 'Collapsible',
'#options' => $truefalse,
'#default_value' => @$formfields['collapsible'],
);
$form['collapsed'] = array(
'#type' => 'radios',
'#title' => 'Collapsed',
'#options' => $truefalse,
'#default_value' => @$formfields['collapsed'],
);
}
$form['weight'] = array(
'#type' => 'select',
'#title' => 'Weight',
'#options' => $weights,
'#default_value' => @$formfields['weight'],
'#description' => 'Higher values appear near at the top of the form, lower values at the bottom.',
);
$form['weight_old'] = array(
'#type' => 'item',
'#title' => 'Original Weight',
'#value' => @$originalfields['weight'],
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Save',
);
$form['reset'] = array(
'#type' => 'submit',
'#value' => 'Reset',
);
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_values = $form_state
->getValues();
$formid = $form_values['formid'];
$fieldname = $form_values['fieldname'];
$formarray = formdefaults_getform($formid);
$baseform = $formarray;
// set the form values
if ($_POST['op'] == 'Reset') {
unset($formarray[$fieldname]);
}
else {
if ($form_values['type'] == 'markup') {
$formarray[$fieldname]['value'] = $form_values['value']['value'];
$formarray[$fieldname]['input_format'] = $form_values['value']['format'];
}
else {
$formarray[$fieldname]['title'] = $form_values['title'];
$formarray[$fieldname]['description'] = $form_values['description'];
}
if (@$form_values['collapsible'] === '') {
unset($formarray[$fieldname]['collapsible']);
}
else {
$formarray[$fieldname]['collapsible'] = @$form_values['collapsible'];
}
if (@$form_values['collapsed'] === '') {
unset($formarray[$fieldname]['collapsed']);
}
else {
$formarray[$fieldname]['collapsed'] = @$form_values['collapsed'];
}
$formarray[$fieldname]['hide_it'] = $form_values['hide_it'];
$formarray[$fieldname]['weight'] = $form_values['weight'];
$formarray[$fieldname]['type'] = $form_values['type'];
}
$helper = new FormDefaultsHelper();
$helper
->saveForm($formid, $formarray);
$form_state
->setRedirect('formdefaults.edit_w_formid', array(
'formid' => $formid,
));
}
}
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 | |
EditFieldForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
EditFieldForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
EditFieldForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
EditFieldForm:: |
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. |