Skip to content
Snippets Groups Projects
FieldGroupAddForm.php 8.65 KiB
Newer Older
bcweaver's avatar
bcweaver committed
<?php

namespace Drupal\field_group\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
bcweaver's avatar
bcweaver committed
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field_group\FieldGroupFormatterPluginManager;
bcweaver's avatar
bcweaver committed
use Drupal\field_group\FieldgroupUi;
use Drupal\field_group\FormatterHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;
bcweaver's avatar
bcweaver committed

/**
 * Provides a form for adding a fieldgroup to a bundle.
 */
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;

  /**
   * The field group formatter plugin manager.
   *
   * @var \Drupal\field_group\FieldGroupFormatterPluginManager
   */
  protected $fieldGroupFormatterPluginManager;

  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * FieldGroupAddForm constructor.
   *
   * @param \Drupal\field_group\FieldGroupFormatterPluginManager $fieldGroupFormatterPluginManager
   *   The field group formatter plugin manager.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger.
   */
  public function __construct(FieldGroupFormatterPluginManager $fieldGroupFormatterPluginManager, MessengerInterface $messenger) {
    $this->fieldGroupFormatterPluginManager = $fieldGroupFormatterPluginManager;
    $this->messenger = $messenger;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('plugin.manager.field_group.formatters'),
      $container->get('messenger')
    );
  }

bcweaver's avatar
bcweaver committed
  /**
   * {@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) {

    $this->entityTypeId = $entity_type_id;
    $this->bundle = $bundle;
    $this->context = $context;

bcweaver's avatar
bcweaver committed
    if ($context == 'form') {
      $this->mode = $this->getRequest()->get('form_mode_name');
bcweaver's avatar
bcweaver committed
    }
    else {
      $this->mode = $this->getRequest()->get('view_mode_name');
bcweaver's avatar
bcweaver committed
    }

    if (empty($this->mode)) {
      $this->mode = 'default';
    }

    if (!$form_state->get('step')) {
      $form_state->set('step', 'formatter');
    }
    $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.
   */
  public function buildFormatterSelectionForm(array &$form, FormStateInterface $form_state) {
bcweaver's avatar
bcweaver committed

    // Gather group formatters.
    $formatter_options = FormatterHelper::formatterOptions($this->context);
    $form['add'] = [
bcweaver's avatar
bcweaver committed
      '#type' => 'container',
      '#attributes' => ['class' => ['form--inline', 'clearfix']],
    ];
bcweaver's avatar
bcweaver committed

    $form['add']['group_formatter'] = [
bcweaver's avatar
bcweaver committed
      '#type' => 'select',
      '#title' => $this->t('Add a new group'),
      '#options' => $formatter_options,
      '#empty_option' => $this->t('- Select a field group type -'),
bcweaver's avatar
bcweaver committed
      '#required' => TRUE,
bcweaver's avatar
bcweaver committed

    // Field label and field_name.
    $form['new_group_wrapper'] = [
bcweaver's avatar
bcweaver committed
      '#type' => 'container',
      '#states' => [
        '!visible' => [
          ':input[name="group_formatter"]' => ['value' => ''],
        ],
      ],
    ];
    $form['new_group_wrapper']['label'] = [
bcweaver's avatar
bcweaver committed
      '#type' => 'textfield',
      '#title' => $this->t('Label'),
      '#size' => 15,
      '#required' => TRUE,
bcweaver's avatar
bcweaver committed

    $form['new_group_wrapper']['group_name'] = [
bcweaver's avatar
bcweaver committed
      '#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' => [
        'source' => ['new_group_wrapper', 'label'],
        'exists' => [$this, 'groupNameExists'],
      ],
bcweaver's avatar
bcweaver committed
      '#required' => TRUE,
bcweaver's avatar
bcweaver committed

    $form['actions'] = ['#type' => 'actions'];
    $form['actions']['submit'] = [
bcweaver's avatar
bcweaver committed
      '#type' => 'submit',
      '#value' => $this->t('Save and continue'),
      '#button_type' => 'primary',
      '#validate' => [
        [$this, 'validateFormatterSelection'],
      ],
    ];
bcweaver's avatar
bcweaver committed

    $form['#attached']['library'][] = 'field_ui/drupal.field_ui';
  }

  /**
   * Build the formatter configuration form.
   */
  public function buildConfigurationForm(array &$form, FormStateInterface $form_state) {
bcweaver's avatar
bcweaver committed

    $group = new \stdClass();
    $group->context = $this->context;
    $group->entity_type = $this->entityTypeId;
    $group->bundle = $this->bundle;
    $group->mode = $this->mode;

    $manager = $this->fieldGroupFormatterPluginManager;
    $plugin = $manager->getInstance([
bcweaver's avatar
bcweaver committed
      'format_type' => $form_state->getValue('group_formatter'),
      'configuration' => [
        'label' => $form_state->getValue('label'),
      ],
      'group' => $group,
bcweaver's avatar
bcweaver committed

    $form['format_settings'] = [
bcweaver's avatar
bcweaver committed
      '#type' => 'container',
      '#tree' => TRUE,
bcweaver's avatar
bcweaver committed

    $form['format_settings'] += $plugin->settingsForm();

    $form['actions'] = ['#type' => 'actions'];
    $form['actions']['submit'] = [
bcweaver's avatar
bcweaver committed
      '#type' => 'submit',
      '#value' => $this->t('Create group'),
      '#button_type' => 'primary',
bcweaver's avatar
bcweaver committed

  }

  /**
   * 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) [
bcweaver's avatar
bcweaver committed
        'group_name' => $form_state->get('group_name'),
        'entity_type' => $this->entityTypeId,
        'bundle' => $this->bundle,
        'mode' => $this->mode,
        'context' => $this->context,
        'children' => [],
bcweaver's avatar
bcweaver committed
        'parent_name' => '',
        'weight' => 20,
        'format_type' => $form_state->get('group_formatter'),
        'region' => 'hidden',
bcweaver's avatar
bcweaver committed

      $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 += $this->fieldGroupFormatterPluginManager->getDefaultSettings($form_state->get('group_formatter'), $this->context);
bcweaver's avatar
bcweaver committed

      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;
      $this->messenger->addMessage($this->t('New group %label successfully created.', ['%label' => $new_group->label]));
bcweaver's avatar
bcweaver committed

      $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 \Drupal\Core\Form\FormStateInterface $form_state
bcweaver's avatar
bcweaver committed
   *   The current state of the form.
   *
   * @return bool
   *   Whether or not the group machine name is taken.
   */
  public function groupNameExists($value, array $element, FormStateInterface $form_state) {
bcweaver's avatar
bcweaver committed

    // Add the prefix.
    $group_name = self::GROUP_PREFIX . $value;

    return field_group_exists($group_name, $this->entityTypeId, $this->bundle, $this->context, $this->mode);
bcweaver's avatar
bcweaver committed
  }

}