You are here

class EditForm in Form Defaults 8

Hierarchy

Expanded class hierarchy of EditForm

File

src/Form/EditForm.php, line 10

Namespace

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

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
EditForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
EditForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
EditForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
EditForm::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.