Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions og_ui/og_ui.module
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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\OgGroupAudienceHelper;
use Drupal\og_ui\BundleFormAlter;
Expand Down Expand Up @@ -75,18 +76,20 @@ function og_ui_entity_type_save(EntityInterface $entity) {
}

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

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, couldn't hold myself :)

where in the UI to you see the posibility to change the target type?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see

edit_basic_page_content_type___site-install_and_d8_dev_ amitai_amitais-macbook-pro zsh _103x23_and_facebook

I'll open a follow up issue

// @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, OgGroupAudienceHelper::DEFAULT_FIELD)) {
$handler_settings = $field->getSetting('handler_settings');
$save = FALSE;
foreach (['target_type', 'target_bundles'] as $key) {
$entity_key = 'og_' . $key;
if (!isset($handler_settings[$key]) || $entity->$entity_key != $handler_settings[$key]) {
$handler_settings[$key] = $entity->$entity_key;
$field->setSetting('handler_settings', $handler_settings);
$save = TRUE;
}
}
if ($save) {
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();
}
}
Expand Down
45 changes: 35 additions & 10 deletions og_ui/tests/src/Functional/BundleFormAlterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class BundleFormAlterTest extends BrowserTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['entity_test', 'node', 'og_ui'];
public static $modules = ['block_content', 'entity_test', 'node', 'og_ui'];

/**
* An administrator user.
Expand All @@ -53,34 +53,42 @@ public function setUp() {

$this->entityTypeManager = \Drupal::entityTypeManager();

// Log in as an administrator that can manage content types.
$this->adminUser = $this->drupalCreateUser(array('bypass node access', 'administer content types'));
// Log in as an administrator that can manage blocks and content types.
$this->adminUser = $this->drupalCreateUser([
'administer blocks',
'administer content types',
'bypass node access',
]);
$this->drupalLogin($this->adminUser);
}

/**
* Test that group and group content bundles can be created through the UI.
*/
public function testCreate() {
// Create a custom block and define it as a group type. We make sure the
// group and group content are of different entity types so we can test that
// the correct entity type is referenced.
$edit = [
'name' => 'school',
'type' => 'school',
'label' => 'school',
'id' => 'school',
'og_is_group' => 1,
];
$this->drupalGet('admin/structure/types/add');
$this->submitForm($edit, t('Save content type'));
$this->drupalGet('admin/structure/block/block-content/types/add');
$this->submitForm($edit, t('Save'));

$edit = [
'name' => 'class',
'type' => 'class',
'og_group_content_bundle' => 1,
'og_target_type' => 'node',
'og_target_type' => 'block_content',
'og_target_bundles[]' => ['school'],
];
$this->drupalGet('admin/structure/types/add');
$this->submitForm($edit, t('Save content type'));
$this->content = $this->drupalGet('admin/structure/types/manage/class');
$this->assertOptionSelected('edit-og-target-bundles', 'school');
$this->assertTargetType('block_content', 'The target type is set to the "Custom Block" entity type.');
$this->assertTargetBundles(['school' => 'school'], 'The target bundles are set to the "school" bundle.');

// Test that if the target bundles are unselected, the value for the target
Expand All @@ -91,7 +99,7 @@ public function testCreate() {
$edit = [
'name' => 'class',
'og_group_content_bundle' => 1,
'og_target_type' => 'node',
'og_target_type' => 'block_content',
'og_target_bundles[]' => [],
];
$this->drupalGet('admin/structure/types/manage/class');
Expand Down Expand Up @@ -127,7 +135,7 @@ public function testGroupContentAjax() {
*
* @param array|NULL $expected
* The expected value for the target bundles.
* @param $message
* @param string $message
* The message to display with the assertion.
*/
protected function assertTargetBundles($expected, $message) {
Expand All @@ -139,4 +147,21 @@ protected function assertTargetBundles($expected, $message) {
$this->assertEquals($expected, $settings['target_bundles'], $message);
}

/**
* Checks whether the target entity type in the group content is as expected.
*
* @param string $expected
* The expected target entity type.
* @param string $message
* The message to display with the assertion.
*/
protected function assertTargetType($expected, $message) {
/** @var EntityFieldManagerInterface $entity_field_manager */
$entity_field_manager = $this->container->get('entity_field.manager');
$entity_field_manager->clearCachedFieldDefinitions();
$field_definitions = $entity_field_manager->getFieldStorageDefinitions('node');
$setting = $field_definitions['og_group_ref']->getSetting('target_type');

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually prefer to avoid hardcoding 'og_group_ref' - but it won't hold this PR

$this->assertEquals($expected, $setting, $message);
}

}
6 changes: 3 additions & 3 deletions src/Plugin/Field/FieldWidget/OgComplex.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected function formMultipleElements(FieldItemListInterface $items, array &$f
$cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality();
$parents = $form['#parents'];

$target_type = $this->fieldDefinition->getTargetEntityTypeId();
$target_type = $this->fieldDefinition->getFieldStorageDefinition()->getSetting('target_type');
$user_groups = Og::getEntityGroups(User::load(\Drupal::currentUser()->id()));
$user_groups_target_type = isset($user_groups[$target_type]) ? $user_groups[$target_type] : [];
$user_group_ids = array_map(function($group) {
Expand Down Expand Up @@ -229,7 +229,7 @@ protected function otherGroupsWidget(FieldItemListInterface $items, FormStateInt

$delta = 0;

$target_type = $this->fieldDefinition->getTargetEntityTypeId();
$target_type = $this->fieldDefinition->getFieldStorageDefinition()->getSetting('target_type');

$user_groups = Og::getEntityGroups(User::load(\Drupal::currentUser()->id()));
$user_groups_target_type = isset($user_groups[$target_type]) ? $user_groups[$target_type] : [];
Expand Down Expand Up @@ -286,7 +286,7 @@ public function otherGroupsSingle($delta, EntityInterface $entity = NULL, $weigh
'target_id' => [
// @todo Allow this to be configurable with a widget setting.
'#type' => 'entity_autocomplete',
'#target_type' => $this->fieldDefinition->getTargetEntityTypeId(),
'#target_type' => $this->fieldDefinition->getFieldStorageDefinition()->getSetting('target_type'),
'#selection_handler' => 'og:default',
'#selection_settings' => [
'other_groups' => TRUE,
Expand Down
9 changes: 4 additions & 5 deletions src/Plugin/OgFields/AudienceField.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ public function getFieldStorageConfigBaseDefinition(array $values = array()) {
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
'custom_storage' => $this->getEntityType() == 'user',
'settings' => [
'handler' => 'og',
'handler_settings' => [
'target_bundles' => [],
'membership_type' => OgMembershipInterface::TYPE_DEFAULT,
],
'target_type' => $this->getEntityType(),
],
'type' => $this->getEntityType() == 'user' ? 'og_membership_reference' : 'og_standard_reference',
Expand All @@ -53,6 +48,10 @@ public function getFieldConfigBaseDefinition(array $values = array()) {
'description' => $this->t('OG group audience reference field.'),
'display_label' => TRUE,
'label' => $this->t('Groups audience'),
'settings' => [
'handler' => 'og',
'handler_settings' => [],
],
];

return parent::getFieldConfigBaseDefinition($values);
Expand Down
53 changes: 39 additions & 14 deletions tests/src/Functional/OgComplexWidgetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Drupal\Tests\og\Functional;

use Drupal\block_content\Entity\BlockContent;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
Expand All @@ -30,46 +32,57 @@ class OgComplexWidgetTest extends BrowserTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['node', 'og'];
public static $modules = ['block_content', 'node', 'og'];

/**
* {@inheritdoc}
*/
function setUp() {
parent::setUp();

// Create a "group" node type and turn it into a group type.
$this->createContentType(['type' => 'group']);
Og::groupManager()->addGroup('node', 'group');
// Create a "group" bundle on the Custom Block entity type and turn it into
// a group. Note we're not using the Entity Test entity for this since it
// does not have real support for multiple bundles.
BlockContentType::create(['type' => 'group']);
Og::groupManager()->addGroup('block_content', 'group');

// Add a group audience field to the "post" node type, turning it into a
// group content type.
$this->createContentType(['type' => 'post']);
Og::createField(OgGroupAudienceHelper::DEFAULT_FIELD, 'node', 'post');
$settings = [
'field_storage_config' => [
'settings' => [
'target_type' => 'block_content',
],
],
];
Og::createField(OgGroupAudienceHelper::DEFAULT_FIELD, 'node', 'post', $settings);
}

/**
* Tests the "Other Groups" field.
* Tests adding groups with the "Groups audience" and "Other Groups" fields.
*
* @dataProvider ogComplexFieldsProvider
*/
function testOtherGroups() {
function testFields($field, $field_name) {
$admin_user = $this->drupalCreateUser(['administer group', 'access content', 'create post content']);
$group_owner = $this->drupalCreateUser(['access content', 'create post content']);

// Create a group content type owned by the group owner.
$settings = [
$values = [
'type' => 'group',
'uid' => $group_owner->id(),
];
$group = $this->createNode($settings);
$group = BlockContent::create($values);
$group->save();

// Log in as administrator.
$this->drupalLogin($admin_user);

// Create a new post in the group by using the "Other Groups" field in the
// UI.
// Create a new post in the group by using the given field in the UI.
$edit = [
'title[0][value]' => "Group owner's post.",
'other_groups[0][target_id]' => "group ({$group->id()})",
$field_name => "group ({$group->id()})",
];
$this->drupalGet('node/add/post');
$this->submitForm($edit, 'Save');
Expand All @@ -91,8 +104,20 @@ function testOtherGroups() {
// Check that the post references the group correctly.
/** @var OgMembershipReferenceItemList $reference_list */
$reference_list = $post->get(OgGroupAudienceHelper::DEFAULT_FIELD);
$this->assertEquals(1, $reference_list->count(), 'There is 1 reference after adding a group to the "Other Groups" field.');
$this->assertEquals($group->id(), $reference_list->first()->getValue()['target_id'], 'The "Other Groups" field references the correct group.');
$this->assertEquals(1, $reference_list->count(), "There is 1 reference after adding a group to the '$field' field.");
$this->assertEquals($group->id(), $reference_list->first()->getValue()['target_id'], "The '$field' field references the correct group.");
}

/**
* Data provider for ::testFields()
*
* @return array
*/
public function ogComplexFieldsProvider() {
return [
['Groups audience', 'og_group_ref[0][target_id]'],
['Other groups', 'other_groups[0][target_id]'],
];
}

}