You are here

og_ui.module in Organic groups 8

Same filename and directory in other branches
  1. 7.2 og_ui/og_ui.module
  2. 7 og_ui/og_ui.module

File

og_ui/og_ui.module
View source
<?php

/**
 * @file
 * Main functions and hook implementations of the Organic Groups UI module.
 */
declare (strict_types=1);
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Entity\BundleEntityFormBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\og\Og;
use Drupal\og\OgGroupAudienceHelperInterface;
use Drupal\og_ui\BundleFormAlter;

/**
 * Implements hook_form_alter().
 */
function og_ui_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  if ($form_state
    ->getFormObject() instanceof BundleEntityFormBase) {
    (new BundleFormAlter($form_state
      ->getFormObject()
      ->getEntity()))
      ->formAlter($form, $form_state);
  }
}

/**
 * Implements hook_entity_insert().
 */
function og_ui_entity_insert(EntityInterface $entity) {
  og_ui_entity_type_save($entity);
}

/**
 * Implements hook_entity_update().
 */
function og_ui_entity_update(EntityInterface $entity) {
  og_ui_entity_type_save($entity);
}

/**
 * Helper to save group information.
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 *   The entity object.
 */
function og_ui_entity_type_save(EntityInterface $entity) {
  if (!$entity instanceof ConfigEntityBundleBase || !isset($entity->og_is_group)) {
    return;
  }
  $bundle = $entity
    ->id();
  $definition = \Drupal::entityTypeManager()
    ->getDefinition($entity
    ->getEntityTypeId());
  $entity_type_id = $definition
    ->getBundleOf();

  // Add/remove the group itself.
  $is_group = Og::isGroup($entity_type_id, $bundle);
  if ($entity->og_is_group != $is_group) {
    if ($entity->og_is_group) {
      Og::groupTypeManager()
        ->addGroup($entity_type_id, $bundle);
    }
    else {
      Og::groupTypeManager()
        ->removeGroup($entity_type_id, $bundle);
    }
  }

  // Add remove the relevant field.
  $is_group_content = Og::isGroupContent($entity_type_id, $bundle);
  if ($entity->og_group_content_bundle != $is_group_content) {
    if ($entity->og_group_content_bundle) {
      Og::createField(OgGroupAudienceHelperInterface::DEFAULT_FIELD, $entity_type_id, $bundle);
    }
    elseif ($field = FieldConfig::loadByName($entity_type_id, $bundle, OgGroupAudienceHelperInterface::DEFAULT_FIELD)) {
      $field
        ->delete();
      return;
    }
  }

  // Change the field target type and bundle.
  if ($field_storage = FieldStorageConfig::loadByName($entity_type_id, OgGroupAudienceHelperInterface::DEFAULT_FIELD)) {
    $target_type = $field_storage
      ->getSetting('target_type');
    if (!empty($entity->og_target_type) && $entity->og_target_type !== $target_type) {

      // @todo It's probably not possible to change the field storage after the
      //   field has data. We should disable this option in the UI.
      $field_storage
        ->setSetting('target_type', $entity->og_target_type);
      $field_storage
        ->save();
    }
  }
  if ($field = FieldConfig::loadByName($entity_type_id, $bundle, OgGroupAudienceHelperInterface::DEFAULT_FIELD)) {
    $handler_settings = $field
      ->getSetting('handler_settings');
    if (!isset($handler_settings['target_bundles']) || $entity->og_target_bundles != $handler_settings['target_bundles']) {
      $handler_settings['target_bundles'] = $entity->og_target_bundles;
      $field
        ->setSetting('handler_settings', $handler_settings);
      $field
        ->save();
    }
  }
}

Functions

Namesort descending Description
og_ui_entity_insert Implements hook_entity_insert().
og_ui_entity_type_save Helper to save group information.
og_ui_entity_update Implements hook_entity_update().
og_ui_form_alter Implements hook_form_alter().