View source
<?php
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;
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);
}
}
function og_ui_entity_insert(EntityInterface $entity) {
og_ui_entity_type_save($entity);
}
function og_ui_entity_update(EntityInterface $entity) {
og_ui_entity_type_save($entity);
}
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();
$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);
}
}
$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;
}
}
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) {
$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();
}
}
}