You are here

class EditFieldForm in Form Defaults 8

@class EditFieldForm

Form to edit the field title and description.

Hierarchy

Expanded class hierarchy of EditFieldForm

File

src/Form/EditFieldForm.php, line 14

Namespace

Drupal\Formdefaults\Form
View 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

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EditFieldForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
EditFieldForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
EditFieldForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
EditFieldForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 87
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.