You are here

public function MessageForm::buildForm in Message UI 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides EntityForm::buildForm

File

src/Form/MessageForm.php, line 54

Class

MessageForm
Form controller for the message_ui entity edit forms.

Namespace

Drupal\message_ui\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);

  /** @var \Drupal\message\Entity\Message $message */
  $message = $this->entity;
  $template = $this->entityTypeManager
    ->getStorage('message_template')
    ->load($this->entity
    ->bundle());
  if ($this
    ->config('message_ui.settings')
    ->get('show_preview')) {
    $form['text'] = [
      '#type' => 'item',
      '#title' => t('Message template'),
      '#markup' => implode("\n", $template
        ->getText()),
    ];
  }

  // Create the advanced vertical tabs "group".
  $form['advanced'] = [
    '#type' => 'details',
    '#attributes' => [
      'class' => [
        'entity-meta',
      ],
    ],
    '#weight' => 99,
  ];
  $form['owner'] = [
    '#type' => 'fieldset',
    '#title' => t('Owner information'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#group' => 'advanced',
    '#weight' => 90,
    '#attributes' => [
      'class' => [
        'message-form-owner',
      ],
    ],
    '#attached' => [
      'library' => [
        'message_ui/message_ui.message',
      ],
      'drupalSettings' => [
        'message_ui' => [
          'anonymous' => \Drupal::config('message_ui.settings')
            ->get('anonymous'),
        ],
      ],
    ],
  ];
  if (isset($form['uid'])) {
    $form['uid']['#group'] = 'owner';
  }
  if (isset($form['created'])) {
    $form['created']['#group'] = 'owner';
  }

  // @todo: assess the best way to access and create tokens tab from D7.
  $tokens = $message
    ->getArguments();
  $access = $this->account
    ->hasPermission('update tokens') || $this->account
    ->hasPermission('bypass message access control');
  if (!empty($tokens) && $access) {
    $form['tokens'] = [
      '#type' => 'fieldset',
      '#title' => t('Tokens and arguments'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#group' => 'advanced',
      '#weight' => 110,
    ];

    // Give the user an option to update the har coded tokens.
    $form['tokens']['replace_tokens'] = [
      '#type' => 'select',
      '#title' => t('Update tokens value automatically'),
      '#description' => t('By default, the hard coded values will be replaced automatically. If unchecked - you can update their value manually.'),
      '#default_value' => 'no_update',
      '#options' => [
        'no_update' => t("Don't update"),
        'update' => t('Update automatically'),
        'update_manually' => t('Update manually'),
      ],
    ];
    $form['tokens']['values'] = [
      '#type' => 'container',
      '#states' => [
        'visible' => [
          ':input[name="replace_tokens"]' => [
            'value' => 'update_manually',
          ],
        ],
      ],
    ];

    // Build list of fields to update the tokens manually.
    foreach ($message
      ->getArguments() as $name => $value) {
      $form['tokens']['values'][$name] = [
        '#type' => 'textfield',
        '#title' => t("@name's value", [
          '@name' => $name,
        ]),
        '#default_value' => $value,
      ];
    }
  }
  $form['langcode'] = [
    '#title' => $this
      ->t('Language'),
    '#type' => 'language_select',
    '#default_value' => $message
      ->getUntranslated()
      ->language()
      ->getId(),
    '#languages' => Language::STATE_ALL,
    '#access' => $this->languageManager
      ->isMultilingual(),
  ];

  // @todo : add similar to node/from library, adding css for
  // 'message-form-owner' class.
  // $form['#attached']['library'][] = 'node/form';
  return $form;
}