You are here

class FieldGroupAddForm in Field Group 8

Same name and namespace in other branches
  1. 8.3 src/Form/FieldGroupAddForm.php \Drupal\field_group\Form\FieldGroupAddForm

Provides a form for adding a fieldgroup to a bundle.

Hierarchy

Expanded class hierarchy of FieldGroupAddForm

File

src/Form/FieldGroupAddForm.php, line 13

Namespace

Drupal\field_group\Form
View source
class FieldGroupAddForm extends FormBase {

  /**
   * The prefix for groups.
   *
   * @var string
   */
  const GROUP_PREFIX = 'group_';

  /**
   * The name of the entity type.
   *
   * @var string
   */
  protected $entityTypeId;

  /**
   * The entity bundle.
   *
   * @var string
   */
  protected $bundle;

  /**
   * The context for the group.
   *
   * @var string
   */
  protected $context;

  /**
   * The mode for the group.
   *
   * @var string
   */
  protected $mode;

  /**
   * Current step of the form.
   *
   * @var string
   */
  protected $currentStep;

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'field_group_add_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL, $bundle = NULL, $context = NULL) {
    if ($context == 'form') {
      $this->mode = \Drupal::request()
        ->get('form_mode_name');
    }
    else {
      $this->mode = \Drupal::request()
        ->get('view_mode_name');
    }
    if (empty($this->mode)) {
      $this->mode = 'default';
    }
    if (!$form_state
      ->get('context')) {
      $form_state
        ->set('context', $context);
    }
    if (!$form_state
      ->get('entity_type_id')) {
      $form_state
        ->set('entity_type_id', $entity_type_id);
    }
    if (!$form_state
      ->get('bundle')) {
      $form_state
        ->set('bundle', $bundle);
    }
    if (!$form_state
      ->get('step')) {
      $form_state
        ->set('step', 'formatter');
    }
    $this->entityTypeId = $form_state
      ->get('entity_type_id');
    $this->bundle = $form_state
      ->get('bundle');
    $this->context = $form_state
      ->get('context');
    $this->currentStep = $form_state
      ->get('step');
    if ($this->currentStep == 'formatter') {
      $this
        ->buildFormatterSelectionForm($form, $form_state);
    }
    else {
      $this
        ->buildConfigurationForm($form, $form_state);
    }
    return $form;
  }

  /**
   * Build the formatter selection step.
   */
  function buildFormatterSelectionForm(array &$form, FormStateInterface $form_state) {

    // Gather group formatters.
    $formatter_options = \field_group_field_formatter_options($form_state
      ->get('context'));
    $form['add'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'form--inline',
          'clearfix',
        ),
      ),
    );
    $form['add']['group_formatter'] = array(
      '#type' => 'select',
      '#title' => $this
        ->t('Add a new group'),
      '#options' => $formatter_options,
      '#empty_option' => $this
        ->t('- Select a group type -'),
      '#required' => TRUE,
    );

    // Field label and field_name.
    $form['new_group_wrapper'] = array(
      '#type' => 'container',
      '#states' => array(
        '!visible' => array(
          ':input[name="group_formatter"]' => array(
            'value' => '',
          ),
        ),
      ),
    );
    $form['new_group_wrapper']['label'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label'),
      '#size' => 15,
      '#required' => TRUE,
    );
    $form['new_group_wrapper']['group_name'] = array(
      '#type' => 'machine_name',
      '#size' => 15,
      // This field should stay LTR even for RTL languages.
      '#field_prefix' => '<span dir="ltr">' . self::GROUP_PREFIX,
      '#field_suffix' => '</span>&lrm;',
      '#description' => $this
        ->t('A unique machine-readable name containing letters, numbers, and underscores.'),
      '#maxlength' => FieldStorageConfig::NAME_MAX_LENGTH - strlen(self::GROUP_PREFIX),
      '#machine_name' => array(
        'source' => array(
          'new_group_wrapper',
          'label',
        ),
        'exists' => array(
          $this,
          'groupNameExists',
        ),
      ),
      '#required' => TRUE,
    );
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => $this
        ->t('Save and continue'),
      '#button_type' => 'primary',
      '#validate' => array(
        array(
          $this,
          'validateFormatterSelection',
        ),
      ),
    );
    $form['#attached']['library'][] = 'field_ui/drupal.field_ui';
  }

  /**
   * Build the formatter configuration form.
   */
  function buildConfigurationForm(array &$form, FormStateInterface $form_state) {
    $group = new \stdClass();
    $group->context = $this->context;
    $group->entity_type = $this->entityTypeId;
    $group->bundle = $this->bundle;
    $group->mode = $this->mode;
    $manager = \Drupal::service('plugin.manager.field_group.formatters');
    $plugin = $manager
      ->getInstance(array(
      'format_type' => $form_state
        ->getValue('group_formatter'),
      'configuration' => [
        'label' => $form_state
          ->getValue('label'),
      ],
      'group' => $group,
    ));
    $form['format_settings'] = array(
      '#type' => 'container',
      '#tree' => TRUE,
    );
    $form['format_settings'] += $plugin
      ->settingsForm();
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => $this
        ->t('Create group'),
      '#button_type' => 'primary',
    );
  }

  /**
   * Validate the formatter selection step.
   */
  public function validateFormatterSelection(array &$form, FormStateInterface $form_state) {
    $group_name = self::GROUP_PREFIX . $form_state
      ->getValue('group_name');

    // Add the prefix.
    $form_state
      ->setValueForElement($form['new_group_wrapper']['group_name'], $group_name);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    if ($form_state
      ->get('step') == 'formatter') {
      $form_state
        ->set('step', 'configuration');
      $form_state
        ->set('group_label', $form_state
        ->getValue('label'));
      $form_state
        ->set('group_name', $form_state
        ->getValue('group_name'));
      $form_state
        ->set('group_formatter', $form_state
        ->getValue('group_formatter'));
      $form_state
        ->setRebuild();
    }
    else {
      $new_group = (object) array(
        'group_name' => $form_state
          ->get('group_name'),
        'entity_type' => $this->entityTypeId,
        'bundle' => $this->bundle,
        'mode' => $this->mode,
        'context' => $this->context,
        'children' => [],
        'parent_name' => '',
        'weight' => 20,
        'format_type' => $form_state
          ->get('group_formatter'),
      );
      $new_group->format_settings = $form_state
        ->getValue('format_settings');
      $new_group->label = $new_group->format_settings['label'];
      unset($new_group->format_settings['label']);
      $new_group->format_settings += _field_group_get_default_formatter_settings($form_state
        ->get('group_formatter'), $this->context);
      field_group_group_save($new_group);

      // Store new group information for any additional submit handlers.
      $groups_added = $form_state
        ->get('groups_added');
      $groups_added['_add_new_group'] = $new_group->group_name;
      drupal_set_message(t('New group %label successfully created.', array(
        '%label' => $new_group->label,
      )));
      $form_state
        ->setRedirectUrl(FieldgroupUi::getFieldUiRoute($new_group));
      \Drupal::cache()
        ->invalidate('field_groups');
    }
  }

  /**
   * Checks if a group machine name is taken.
   *
   * @param string $value
   *   The machine name, not prefixed.
   * @param array $element
   *   An array containing the structure of the 'group_name' element.
   * @param FormStateInterface $form_state
   *   The current state of the form.
   *
   * @return bool
   *   Whether or not the group machine name is taken.
   */
  public function groupNameExists($value, $element, FormStateInterface $form_state) {

    // Add the prefix.
    $group_name = self::GROUP_PREFIX . $value;
    $entity_type = $form_state
      ->get('entity_type_id');
    $bundle = $form_state
      ->get('bundle');
    $context = $form_state
      ->get('context');
    $mode = $form_state
      ->get('mode');
    return field_group_exists($group_name, $entity_type, $bundle, $context, $mode);
  }

}

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
FieldGroupAddForm::$bundle protected property The entity bundle.
FieldGroupAddForm::$context protected property The context for the group.
FieldGroupAddForm::$currentStep protected property Current step of the form.
FieldGroupAddForm::$entityTypeId protected property The name of the entity type.
FieldGroupAddForm::$mode protected property The mode for the group.
FieldGroupAddForm::buildConfigurationForm function Build the formatter configuration form.
FieldGroupAddForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
FieldGroupAddForm::buildFormatterSelectionForm function Build the formatter selection step.
FieldGroupAddForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
FieldGroupAddForm::groupNameExists public function Checks if a group machine name is taken.
FieldGroupAddForm::GROUP_PREFIX constant The prefix for groups.
FieldGroupAddForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
FieldGroupAddForm::validateFormatterSelection public function Validate the formatter selection step.
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.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
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.