class EditForm in Form Defaults 8
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Formdefaults\Form\EditForm
Expanded class hierarchy of EditForm
File
- src/
Form/ EditForm.php, line 10
Namespace
Drupal\Formdefaults\FormView source
class EditForm extends FormBase {
public function getFormId() {
return 'formdefaults_edit_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$path_args = explode('/', current_path());
$formid = $path_args[1];
$fieldname = $path_args[2];
// Load the form
$data = formdefaults_getform($formid);
$fields = array();
$form['formid'] = array(
'#type' => 'value',
'#value' => $formid,
);
foreach ($data as $f => $field) {
if (strpos($f, '#') !== 0) {
$t = @$field['title'] ? ' - ' . @$field['title'] : '';
$fields[$f] = Link::createFromRoute(t($f . $t), 'formdefaults.edit', [
'formid' => $formid,
'field' => urlencode($f),
]);
}
}
$form['fields'] = array(
'#type' => 'checkboxes',
'#title' => 'Overriden Fields',
'#options' => $fields,
);
$form['add'] = array(
'#type' => 'fieldset',
'#title' => 'Add Fields',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$types = array(
'markup' => 'Markup',
'fieldset' => 'Collapsed fieldset with markup ',
);
$form['add']['field_type'] = array(
'#type' => 'select',
'#title' => 'Type',
'#options' => $types,
'#description' => t('Choose Markup to add a place for instructions that are always seen. Choose collapsed fieldset to add instructions inside an expandable box'),
);
// Weight of
$weight_range = range(-50, 50);
$weights = array(
'unset' => 'unset',
);
foreach ($weight_range as $weight) {
$weights[(string) $weight] = (string) $weight;
}
$form['add']['weight'] = array(
'#type' => 'select',
'#title' => 'Weight',
'#options' => $weights,
'#default_value' => -49,
'#description' => 'Controls placement within the form, -49 is a good header value or 50 is usually a good footer value',
);
$form['add']['add_submit'] = array(
'#type' => 'submit',
'#value' => 'Add',
);
$form['reset'] = array(
'#type' => 'submit',
'#value' => 'Reset Selected',
);
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'];
$formdef = formdefaults_getform($formid);
// Reset fields
if ($_POST['op'] == 'Reset Selected') {
foreach ($form_values['fields'] as $field => $checked) {
if ($checked) {
unset($formdef[$field]);
}
}
// Condense addon array.
if (isset($formdef['#formdefaults_addon_fields'])) {
$addons = (array) $formdef['#formdefaults_addon_fields'];
$new_addons = array();
foreach ($addons as $key => $field) {
if (@$formdef[$key]) {
$i = 'formdefaults_' . count($new_addons);
if ($i != $key) {
$formdef[$i] = $formdef[$key];
unset($formdef[$key]);
if ($formdef[$i . '_markup']) {
$formdef[$i . '_markup'] = $formdef[$key . '_markup'];
unset($formdef[$key . '_markup']);
}
}
$new_addons[$i] = $field;
}
}
$formdef['#formdefaults_addon_fields'] = $new_addons;
}
}
if ($_POST['op'] == 'Add') {
$i = count((array) @$formdef['#formdefaults_addon_fields']);
$key = 'formdefaults_' . $i;
$subkey = $key . '_markup';
$field = array();
$weight = $form_values['weight'];
switch ($form_values['field_type']) {
case "markup":
$field = array(
'#type' => 'markup',
'#markup' => '',
);
$formdef[$key] = array(
'type' => 'markup',
'value' => '<p>Replace with your own markup</p>',
'format' => 0,
'weight' => $weight,
);
break;
case "fieldset":
$field = array(
'#type' => 'fieldset',
'#title' => 'Untitled',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
$subkey => array(
'#type' => 'markup',
'#value' => '',
),
);
$formdef[$key] = array(
'type' => 'fieldset',
'title' => 'Untitled',
'weight' => $weight,
);
$formdef[$subkey] = array(
'type' => 'markup',
'value' => '<p>Replace with your own markup</p>',
);
break;
}
$formdef['#formdefaults_addon_fields'][$key] = $field;
}
$helper = new FormDefaultsHelper();
$helper
->saveForm($formid, $formdef);
}
}
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 | |
EditForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
EditForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
EditForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
EditForm:: |
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. |