You are here

groupmedia.module in Group Media 8.2

Same filename and directory in other branches
  1. 8 groupmedia.module

Allows to associate media content to group.

File

groupmedia.module
View source
<?php

/**
 * @file
 * Allows to associate media content to group.
 */
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\group\Entity\GroupContentInterface;
use Drupal\media\MediaInterface;
use Drupal\media\MediaTypeInterface;

/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function groupmedia_media_type_insert(MediaTypeInterface $media_type) {
  \Drupal::service('plugin.manager.group_content_enabler')
    ->clearCachedDefinitions();
}

/**
 * Implements hook_entity_insert().
 */
function groupmedia_entity_insert(EntityInterface $entity) {

  // React only on Group Content insert, because adding other types do not add
  // group content. Group content will be available through getEntity() method.
  if ($entity instanceof GroupContentInterface) {
    \Drupal::service('groupmedia.attach_group')
      ->attach($entity);
  }
}

/**
 * Implements hook_entity_insert().
 */
function groupmedia_entity_update(EntityInterface $entity) {
  \Drupal::service('groupmedia.attach_group')
    ->attach($entity);
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
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),
    ];
  }
}

/**
 * Implements hook_help().
 */
function groupmedia_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.groupmedia':
      $text = file_get_contents(__DIR__ . '/README.md');
      if (!\Drupal::moduleHandler()
        ->moduleExists('markdown')) {
        return '<pre>' . Html::escape($text) . '</pre>';
      }
      else {

        // Use the Markdown filter to render the README.
        $filter_manager = \Drupal::service('plugin.manager.filter');
        $settings = \Drupal::configFactory()
          ->get('markdown.settings')
          ->getRawData();
        $config = [
          'settings' => $settings,
        ];
        $filter = $filter_manager
          ->createInstance('markdown', $config);
        return $filter
          ->process($text, 'en');
      }
  }
  return NULL;
}

/**
 * Implements hook_entity_operation().
 */
function groupmedia_entity_operation(EntityInterface $entity) {
  $operations = [];
  if ($entity
    ->getEntityTypeId() == 'group' && \Drupal::moduleHandler()
    ->moduleExists('views')) {

    /** @var \Drupal\group\Entity\GroupInterface $entity */
    if (!$entity
      ->hasPermission('access group_media overview', \Drupal::currentUser())) {

      // Exit early without checking group content plugins.
      return $operations;
    }

    // Get the list of the group_content plugins enabled for this group type.
    $plugin_ids = $entity
      ->getGroupType()
      ->getInstalledContentPlugins()
      ->getInstanceIds();

    // Check if there is any media group_content plugin enabled.
    $has_media = FALSE;
    foreach ($plugin_ids as $plugin_id) {
      if (strpos($plugin_id, 'group_media:') === 0) {

        // We need at least 1 enabled plugin to have the link.
        $has_media = TRUE;
        break;
      }
    }

    /** @var \Drupal\group\Entity\GroupInterface $entity */
    if ($has_media) {

      /** @var \Symfony\Component\Routing\RouterInterface $router */
      $router = \Drupal::service('router.no_access_checks');
      if ($router
        ->getRouteCollection()
        ->get('view.group_media.page_1') !== NULL) {
        $operations['media'] = [
          'title' => t('Media'),
          'weight' => 22,
          'url' => Url::fromRoute('view.group_media.page_1', [
            'group' => $entity
              ->id(),
          ]),
        ];
      }
    }
  }
  return $operations;
}

/**
 * Implements hook_groupmedia_finder_add_alter().
 */
function groupmedia_groupmedia_finder_add_alter(&$result, MediaInterface $media, array &$context) {
  $bundles = \Drupal::config('groupmedia.settings')
    ->get('bundles');
  if (in_array($media
    ->bundle(), $bundles, TRUE)) {
    $result[] = FALSE;
  }
}