You are here

public function OpignoPrivateMessageThreadForm::buildForm in Opigno messaging 3.x

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 FormInterface::buildForm

File

src/Form/OpignoPrivateMessageThreadForm.php, line 150

Class

OpignoPrivateMessageThreadForm
Custom form to create/edit private message thread.

Namespace

Drupal\opigno_messaging\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, int $tid = 0) {
  $plugin_instance = $this->lpMembersManager
    ->createInstance('recipients_plugin');
  if (!$this->currentUser instanceof UserInterface || !$plugin_instance instanceof RecipientsPlugin) {
    return [];
  }

  // Don't render form for existing 1-to-1 messages.
  $thread = NULL;
  if ($tid && $this->threadStorage instanceof EntityStorageInterface) {
    $thread = $this->threadStorage
      ->load($tid);
    if (!$thread instanceof PrivateMessageThreadInterface || count($thread
      ->getMembers()) <= 2) {
      return [];
    }
  }

  // Check if the thread is a group discussion.
  $is_group = $thread instanceof PrivateMessageThreadInterface && $thread
    ->hasField('field_create_group') && $thread
    ->get('field_create_group')
    ->getString();
  $form['#attributes']['class'] = $is_group ? [
    'opigno-pm-thread-form__edit',
  ] : [
    'opigno-pm-thread-form__add',
  ];

  // Add the placeholder for the status messages.
  $form['status_messages_container'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'opigno-status-messages-container',
      ],
    ],
    '#weight' => -50,
  ];

  // Add the "Create a group" checkbox.
  $form['create_group'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Create a group'),
    '#default_value' => $is_group,
    '#weight' => -20,
    '#attributes' => [
      'class' => [
        'checkbox-slider',
      ],
    ],
  ];

  // Hide the checkbox on the thread editing form.
  if ($is_group) {
    $form['create_group']['#prefix'] = '<div class="hidden">';
    $form['create_group']['#suffix'] = '</div>';
  }
  $form['group_data_container'] = [
    '#type' => 'container',
    '#tree' => FALSE,
    '#states' => [
      'visible' => [
        ':input[name="create_group"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
    '#weight' => -15,
  ];

  // Add the subject.
  $form['group_data_container']['subject'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Subject'),
    '#placeholder' => $this
      ->t('Enter a subject'),
    '#default_value' => $is_group ? $thread
      ->get('field_pm_subject')
      ->getString() : '',
    '#maxlength' => 128,
  ];

  // The group picture field.
  $timestamp = strtotime('now');
  $year = $this->dateFormatter
    ->format($timestamp, 'custom', 'Y');
  $month = $this->dateFormatter
    ->format($timestamp, 'custom', 'm');
  $form['group_data_container']['image'] = [
    '#type' => 'managed_file',
    '#title' => $this
      ->t('Picture'),
    '#upload_validators' => [
      'file_validate_extensions' => [
        'gif png jpg jpeg',
      ],
    ],
    '#upload_location' => "public://{$year}-{$month}",
    '#theme' => 'image_widget',
    '#preview_image_style' => 'private_message_group_upload',
    '#default_value' => $is_group && !$thread
      ->get('field_image')
      ->isEmpty() ? [
      $thread
        ->get('field_image')->target_id,
    ] : '',
    '#multiple' => FALSE,
  ];

  // Add members selection tool and message field.
  $plugin_instance
    ->getMembersForm($form, $form_state, $this->currentUser, $is_group);
  if ($is_group) {
    $form['users_to_send']['#default_value'] = $thread
      ->getMembersId();

    // Display the extra checkbox to manage members on thread edit form.
    $form['edit_members'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Manage participants'),
      '#ajax' => [
        'callback' => '::showMembersAjax',
        'event' => 'change',
      ],
      '#weight' => -5,
      '#attributes' => [
        'class' => [
          'checkbox-slider',
        ],
      ],
    ];
  }
  if (!$tid) {
    $form['message'] = [
      '#type' => 'text_format',
      '#title' => $this
        ->t('Message'),
      '#title_display' => 'invisible',
      '#format' => 'basic_html',
      '#required' => TRUE,
    ];
  }

  // Actions.
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $is_group ? $this
      ->t('Save') : $this
      ->t('Send'),
    '#ajax' => [
      'callback' => '::ajaxSubmit',
    ],
    '#attributes' => [
      'class' => [
        'use-ajax-submit',
      ],
    ],
  ];
  return $form;
}