You are here

function groupmedia_form_media_form_alter in Group Media 8.2

Implements hook_form_BASE_FORM_ID_alter().

File

./groupmedia.module, line 46
Allows to associate media content to group.

Code

function groupmedia_form_media_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  /** @var \Drupal\Core\Entity\ContentEntityInterface $media */
  $media = $form_state
    ->getFormObject()
    ->getEntity();
  if (!$media
    ->isNew()) {
    $groups = [];
    $group_contents = \Drupal::entityTypeManager()
      ->getStorage('group_content')
      ->loadByEntity($media);
    foreach ($group_contents as $group_content) {
      $group = $group_content
        ->getGroup();
      $groups[] = Link::fromTextAndUrl($group
        ->label(), $group
        ->toUrl('canonical', [
        'attributes' => [
          'target' => '_blank',
        ],
      ]));
    }
    $form['group_information'] = [
      '#type' => 'details',
      '#title' => t('Groups'),
      '#access' => !empty($groups),
      '#group' => 'advanced',
      '#weight' => 100,
      '#open' => FALSE,
    ];
    $form['group_information']['group_list'] = [
      '#theme' => 'item_list',
      '#items' => $groups,
      '#title' => t('The list of groups media item belongs to:'),
      '#access' => !empty($groups),
    ];
  }
}