You are here

public function Subscriptions::buildForm in Message Subscribe 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 FormInterface::buildForm

File

message_subscribe_ui/src/Plugin/Block/Subscriptions.php, line 151

Class

Subscriptions
An entity subscriptions block.

Namespace

Drupal\message_subscribe_ui\Plugin\Block

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = [];

  // Add the entity being viewed.
  $entity = $this
    ->getCurrentEntity();
  $entities = [
    $entity,
  ];
  $entities += $entity
    ->referencedEntities();
  $form['description'] = [
    '#type' => 'markup',
    '#markup' => $this
      ->t('Manage all <a href=":url">subscriptions</a>.', [
      ':url' => Url::fromRoute('message_subscribe_ui.tab', [
        'user' => \Drupal::currentUser()
          ->id(),
      ])
        ->toString(),
    ]),
  ];
  $form['subscriptions'] = [
    '#type' => 'container',
    '#tree' => TRUE,
  ];
  foreach ($entities as $referenced_entity) {

    // Verify user can access the referenced entity.
    if ($referenced_entity
      ->access('view')) {
      $flags = $this->subscribers
        ->getFlags($referenced_entity
        ->getEntityTypeId(), $referenced_entity
        ->bundle(), $this->currentUser);
      if (!empty($flags)) {

        /** @var \Drupal\flag\FlagInterface $flag */

        // @todo Support multiple subscription flags per-entity if there is
        // such a use-case.
        $flag = reset($flags);
        $form['subscriptions'][$referenced_entity
          ->getEntityTypeId()][$referenced_entity
          ->id()] = [
          '#type' => 'checkbox',
          // @todo Determine how to use the flag label/value.
          '#title' => $this
            ->getLabel($referenced_entity),
          '#default_value' => !empty($this->flagService
            ->getFlagging($flag, $referenced_entity, $this->currentUser)),
          '#flags' => $flags,
          '#entity' => $referenced_entity,
        ];
      }
    }
  }
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
  ];
  return $form;
}