View source
<?php
use Drupal\Core\Database\Database;
use Drupal\Core\Link;
use Drupal\Component\Utility\UrlHelper;
function formdefaults_help($section) {
switch ($section) {
case 'admin/modules#description':
return t("Lets the site administrator edit the description, title and markup attributes of a form.");
}
}
function _formdefaults_addon_fields($form_def, &$form) {
if (@$form_def['formdefaults_header']) {
$form['formdefaults_header'] = array(
'#type' => 'markup',
'#value' => '',
'#weight' => -50,
);
}
if (@$form_def['formdefaults_footer']) {
$form['formdefaults_footer'] = array(
'#type' => 'markup',
'#value' => '',
'#weight' => 50,
);
}
foreach (@(array) $form_def['#formdefaults_addon_fields'] as $key => $field) {
$form[$key] = $field;
}
}
function formdefaults_form_alter(&$form, &$form_state, $formid) {
$savedform = formdefaults_getform($formid);
_formdefaults_addon_fields($savedform, $form);
$enabled = @($_SESSION['formdefaults_enabled'] && $formid != 'formdefaults_edit');
$formfieldsarray = array();
if (Drupal::currentUser()
->hasPermission('change form labels') && $enabled) {
$forms = @$_SESSION['formdefaults_forms'];
foreach ($form as $fieldname => $value) {
_formdefaults_get_field($formid, $fieldname, $form[$fieldname], $formfieldsarray);
}
$forms[$formid] = $formfieldsarray;
$_SESSION['formdefaults_forms'] = $forms;
}
if ($savedform) {
foreach ($form as $fieldname => $value) {
formdefaults_alterform($formid, $fieldname, $form[$fieldname], $savedform);
}
}
if ($enabled) {
$link = Link::createFromRoute('[' . t('edit form') . ']', 'formdefaults.edit_w_formid', [
'formid' => $formid,
])
->toString();
$form['formdefaults_edit_form'] = array(
'#type' => 'markup',
'#weight' => -50,
'#markup' => '<p>' . $link . '</p>',
);
}
}
function _formdefaults_get_field($formid, $fieldname, &$fieldvalue, &$fieldarray) {
if (is_array($fieldvalue) && !(strpos($fieldname, '#') === 0)) {
$query = array();
$query['destination'] = \Drupal::request()->attributes
->get('_system_path');
if (is_array($query)) {
$query = UrlHelper::buildQuery($query);
$options['query'] = $query;
}
$options = array();
$type = @$fieldvalue['#type'];
$fieldarray[$fieldname]['type'] = $type;
if ($type != 'submit' && $type != 'hidden') {
if (array_key_exists('#weight', $fieldvalue)) {
$fieldarray[$fieldname]['weight'] = $fieldvalue['#weight'];
}
else {
$fieldarray[$fieldname]['weight'] = 'unset';
}
if ($type == 'markup') {
$link = Link::createFromRoute('[' . t('edit markup') . ']', 'formdefaults.edit', [
'formid' => $formid,
'field' => $fieldname,
])
->toString();
$fieldarray[$fieldname]['value'] = @$fieldvalue['#markup'];
$fieldvalue['#markup'] = $link . ' ' . @$fieldvalue['#markup'];
}
else {
$fieldarray[$fieldname]['title'] = @$fieldvalue['#title'];
$fieldarray[$fieldname]['description'] = @$fieldvalue['#description'];
$fieldvalue['#suffix'] = Link::createFromRoute('[' . t('edit @type', [
'@type' => @$fieldvalue['#type'],
]) . ']', 'formdefaults.edit', [
'formid' => $formid,
'field' => $fieldname,
])
->toString();
}
}
foreach ($fieldvalue as $key => $value) {
if (!(strpos($key, '#') === 0)) {
_formdefaults_get_field($formid, $key, $fieldvalue[$key], $fieldarray);
}
}
}
}
function _formdefaults_hide_field(&$fieldvalue) {
switch ($fieldvalue['#type']) {
case "fieldset":
case "markup":
$h_type = 'value';
$fieldvalue['#value'] = '';
break;
default:
$h_type = 'value';
break;
}
$fieldvalue['#type'] = $h_type;
if (!array_key_exists('#value', $fieldvalue)) {
$fieldvalue['#value'] = $fieldvalue['#default_value'];
}
}
function _formdefaults_hide_recursive(&$form_element) {
foreach ((array) $form_element as $key => $field) {
if (strpos($key, '#') === FALSE) {
_formdefaults_hide_recursive($form_element[$key]);
}
}
_formdefaults_hide_field($form_element);
}
function formdefaults_alterform($formid, $fieldname, &$fieldvalue, $formreplace) {
$query = array();
$query['destination'] = $_GET['q'];
$enabled = @$_SESSION['formdefaults_enabled'] && $formid != 'formdefaults_edit';
if (is_array($fieldvalue) && !(strpos($fieldname, '#') === 0)) {
if (is_array(@$formreplace[$fieldname])) {
$fo = $formreplace[$fieldname];
if (@$formreplace[$fieldname]['hide_it']) {
_formdefaults_hide_recursive($fieldvalue);
}
if (@$fo['weight'] != 'unset') {
$fieldvalue['#weight'] = $fo['weight'];
}
if (@$fieldvalue['#type'] == 'fieldset' && array_key_exists('collapsible', $fo)) {
$fieldvalue['#collapsible'] = $fo['collapsible'];
}
if (@$fieldvalue['#type'] == 'fieldset' && array_key_exists('collapsed', $fo)) {
$fieldvalue['#collapsed'] = $fo['collapsed'];
}
if (@$fieldvalue['#type'] == 'markup') {
$fieldvalue['#markup'] = check_markup($formreplace[$fieldname]['value'], @$formreplace[$fieldname]['input_format'], FALSE);
}
else {
$fieldvalue['#title'] = @$fo['title'];
$fieldvalue['#description'] = @$fo['description'];
}
$url
->setOption('query', $query);
if (Drupal::currentUser()
->hasPermission('change form labels') && $enabled) {
if ($fieldvalue['#type'] != 'markup') {
$fieldvalue['#suffix'] = Link::createFromRoute('[' . t('edit') . ' ' . t($fieldvalue['#type']) . ']', 'formdefaults.edit', [
'formid' => $formid,
'field' => $fieldname,
])
->toString();
}
else {
$link = Link::createFromRoute('[' . t('edit markup') . ']', 'formdefaults.edit', [
'formid' => $formid,
'field' => $fieldname,
])
->toString();
$fieldvalue['#markup'] = $link . ' ' . @$fieldvalue['#markup'];
}
}
}
foreach ($fieldvalue as $key => $value) {
if (!(strpos($key, '#') === 0)) {
formdefaults_alterform($formid, $key, $fieldvalue[$key], $formreplace);
}
}
}
}
function formdefaults_getform($formid) {
$result = Database::getConnection()
->query('SELECT * FROM {formdefaults_forms} WHERE formid = :formid', [
':formid' => $formid,
]);
$formarray = array();
foreach ($result as $form) {
$formarray = unserialize($form->formdata);
}
return $formarray;
}