og-8.x-1.x-dev/og_ui/og_ui.module

og_ui/og_ui.module
<?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;

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

  $entity_type = $form_state->getFormObject()->getEntity();
  // To avoid insanity, a group membership cannot be a group or group content.
  if ($entity_type->getEntityTypeId() === 'og_membership_type') {
    return;
  }

  \Drupal::service('og_ui.bundle_entity_form_alter')->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 when a bundle entity is saved.
 *
 * @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);
    }
  }

  if ($entity->og_is_group) {
    $group_membership_type = $entity->og_membership_type;
    Og::groupTypeManager()->setGroupDefaultMembershipType($entity_type_id, $bundle, $group_membership_type);
  }
  elseif ($is_group) {
    // Remove the membership type setting, as this bundle has been demoted
    // from being a group.
    Og::groupTypeManager()->removeGroupDefaultMembershipType($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();
    }
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc