Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Referencing a different entity type is broken in the complex widget #163

Merged
merged 12 commits into from
Mar 31, 2016

Conversation

idimopoulos
Copy link

$this->fieldDefinition->getTargetEntityTypeId() returns the type of the entity that the field is attached to.

This is a logical mistake. Target type should be the group type.
Changed to $this->getFieldSetting('target_type') in order to obtaing the target type of the original widget field.
Similar to: #120

To reproduce, you can create an entity type other than node or user and make it a og group.
Then create a content type and make it a candidate member of that group.
Create some dummy group content.
Try to add content as an admin of og in order to get the "Other groups" field.
The functionality should be problematic because the target type of the "Other group" field is set to node (our content type) instead of the group entity type.
if you enter something and submit, the site also breaks.

…he entity that the field is attached to.

This is a logical mistake. Target type should be the group type.
Changed to $this->getFieldSetting('target_type') in order to obtaing the target type of the original widget field.
Similar to: amitaibu#120
@pfrenssen
Copy link
Collaborator

Can you be a little bit more specific in the instructions how to reproduce the problem? When I try to replicate it I indeed hit a bug, but it is different than what you describe, and the fix doesn't solve it. A test case would be even better :)

Here's what I tried:

  1. Do a clean install using the standard profile.
  2. Enable og and og_ui.
  3. Log in as UID1.
  4. Go to admin/structure/block/block-content/manage/basic and check the option to make the "basic" custom block into a group.
  5. Go to block/add and create a new block called "Group".
  6. Go to admin/structure/types/manage/article and set the "article" node type to be group content, with target type "Custom block" and bundle "Basic block" (I specifically selected "Basic block" in the list, because leaving it empty has a known bug).
  7. Go to admin/structure/types/manage/article/form-display and verify the group audience field is visible.
  8. Go to node/add/article and start typing "Group" in the "Other groups" field.

Result: the AJAX call fails with the following error:

Uncaught PHP Exception InvalidArgumentException: "Placeholders must have a trailing [] if they are to be expanded with an array of values." at core/lib/Drupal/Core/Database/Connection.php line 719

From a first look this is due to my field config being incorrectly saved, I see this in field.field.node.article.og_group_ref:

settings:
  handler: 'default:node'
  handler_settings:
    target_type: block_content
    target_bundles: null
  access_override: false

That default:node handler is not correct I think. Also the target bundles are empty even though I specifically selected them.

@pfrenssen
Copy link
Collaborator

I manually fixed my field settings to the following:

     settings: [
       "handler" => "og:default",
       "handler_settings" => [
         "target_type" => "block_content",
         "target_bundles" => [
           "basic" => "basic",
         ],
       ],
     ],

But I still couldn't reference my Group custom block. I digged into OgComplex and there were a couple more instances where the wrong target type was retrieved. I fixed them in the commit above.

With these changes I can reference the custom blocks.

Two things to do:

  1. We should create a followup ticket to fix the incorrect saving of the field handler.
  2. Create a test for this. We can look at Port OgComplexWidgetTest #140, that already has done some work on testing OgComplex. We can probably lift some code from there.

@pfrenssen
Copy link
Collaborator

  1. Fix the test. Seems like the field handler settings are saved differently in the test. The 'handler_settings' entry seems to be missing.

pfrenssen pushed a commit that referenced this pull request Mar 16, 2016
@pfrenssen
Copy link
Collaborator

There is still a bug here. The "Other Groups" field works now, but the "Groups Audience" field still doesn't work as expected.

@pfrenssen
Copy link
Collaborator

This is how $this->configuration looks in OgSelection::getSelectionHandler() when using the "Groups Audience" field. This looks like the settings that come directly from the field definition:

array (
  'target_type' => 'block_content',
  'handler' => 'og:default',
  'handler_settings' => 
  array (
    'target_type' => 'node',
    'target_bundles' => 
    array (
      'group' => 'group',
    ),
    'field_mode' => 'default',
  ),
)

This is how $this->configuration looks in OgSelection::getSelectionHandler() when using the "Other Groups" field. This looks like the settings that are generated in OgComplex::otherGroupsSingle():

array (
  'target_type' => 'node',
  'handler' => 'og:default',
  'handler_settings' => 
  array (
    'other_groups' => true,
    'field_mode' => 'admin',
  ),
)

We need to decide on either format.

@pfrenssen pfrenssen changed the title Change wrong target_type in OgComplex.php Referencing a different entity type is broken in the complex widget Mar 16, 2016
@amitaibu
Copy link
Owner

The first block code looks like the correct one

array (
  'target_type' => 'block_content',
  'handler' => 'og:default',
  'handler_settings' => 
  array (
    'target_type' => 'node',
    'target_bundles' => 
    array (
      'group' => 'group',
    ),
    'field_mode' => 'default',
  ),
)

Further more I believe OgComplex::otherGroupsSingle is going to be soon removed by #161

(btw, just to set the common language -- a single (group audience) field has to "field modes" default and admin, or could be called "my groups" and "other groups")

@pfrenssen
Copy link
Collaborator

Ok let's wait until #161 is in.

@pfrenssen
Copy link
Collaborator

Going to continue on this. I'm currently blocked on #162 on the saving of target_bundles, and the solution I'm working on is causing the OgComplexTest to fail. I need some fixes in the OgComplex widget, meaning I need either this or #161 to be finished. I had a look at #161 but it still needs some work. This doesn't really interfere with that either it seems, the merge conflicts will be pretty minimal.

@pfrenssen
Copy link
Collaborator

I think it's fixed now. Still needs a test.

@pfrenssen
Copy link
Collaborator

I started on the test by using the Custom Block entity type instead of Node to create the group. So far it seems to work fine for the Other Groups field. Still need to test the Groups Audience field as mentioned in one of my previous comments.

I am only working for two days this week because of some religious holiday, and I will have to work on other tasks for the moment. I will probably be able to pick this back up next Monday at the earliest.

@pfrenssen
Copy link
Collaborator

Picking this back up since it is blocking my work in #162

@pfrenssen
Copy link
Collaborator

I now have the strange situation that if I follow the steps from the first comment manually it doesn't work as expected, but when I run the test it does.

It appears that when the group audience field is added in the setup of the test the field storage settings are correct, but when I add it through the interface it is incorrectly set to "node" instead of "block_content".

I compared it with a vanilla entityreference field to make sure that my assumptions are correct.

This is the field storage config of the group audience field (field.storage.node.og_group_ref) as created through the UI:

langcode: en
status: true
dependencies:
  module:
    - node
    - og
id: node.og_group_ref
field_name: og_group_ref
entity_type: node
type: og_standard_reference
settings:
  target_type: node // <----- should be block_content
module: og
locked: false
cardinality: -1
translatable: true
indexes: {  }
persist_with_no_fields: false
custom_storage: false

Here is the config of the vanilla entity reference field I created to compare:

langcode: en
status: true
dependencies:
  module:
    - block_content
    - node
id: node.field_entityreference
field_name: field_entityreference
entity_type: node
type: entity_reference
settings:
  target_type: block_content // <---- here it is
module: core
locked: false
cardinality: 1
translatable: true
indexes: {  }
persist_with_no_fields: false
custom_storage: false

I also checked the field instance config, the target type is erroneously set here it seems. This is field.field.node.article.og_group_ref:

langcode: en
status: true
dependencies:
  config:
    - field.storage.node.og_group_ref
    - node.type.article
  module:
    - og
id: node.article.og_group_ref
field_name: og_group_ref
entity_type: node
bundle: article
label: 'Groups audience'
description: 'OG group audience reference field.'
required: false
translatable: true
default_value: {  }
default_value_callback: ''
settings:
  handler: 'og:default'
  handler_settings:
    target_type: block_content // <---- this is the target_type we are looking for
    target_bundles:
      basic: basic
  access_override: false
field_type: og_standard_reference

As reference, here is the vanilla entityreference equivalent:

langcode: en
status: true
dependencies:
  config:
    - block_content.type.basic
    - field.storage.node.field_entityreference
    - node.type.article
id: node.article.field_entityreference
field_name: field_entityreference
entity_type: node
bundle: article
label: entityreference
description: ''
required: false
translatable: false
default_value: {  }
default_value_callback: ''
settings:
  handler: 'default:block_content'
  handler_settings:
    target_bundles:  // <--- only target_bundle info here, no target_type
      basic: basic
    sort:
      field: _none
field_type: entity_reference

@pfrenssen
Copy link
Collaborator

Fixed it in the UI. We can also better add a test for this, to check that the fields are correctly saved when created through the UI.

So remaining work here:

  1. Test the "Groups Audience" field.
  2. Test that the target entity is correctly set for the Group Audience field when creating a group content type through the UI.

@pfrenssen
Copy link
Collaborator

To prove that the tests correctly detect the bug I created a temporary branch that only contains the tests: https://github.com/amitaibu/og/tree/PR163-test-only

It is currently being tested at https://travis-ci.org/amitaibu/og/builds/119533962 - if all goes well these tests should fail.

@pfrenssen
Copy link
Collaborator

Great, both tests fail as expected in the PR163-test-only branch.

The first test fails with a 500 error which is due to uncaught InvalidArgumentException that I mentioned in my first comment:

Drupal\Tests\og\Functional\OgComplexWidgetTest::testFields with data set #1 ('Other groups', 'other_groups[0][target_id]')
Behat\Mink\Exception\ExpectationException: Current response status code is 500, but 200 expected.

The second one correctly fails since the target entity type is not set correctly:

Drupal\og_ui\Tests\BundleFormAlterTest::testCreate
The target type is set to the "Custom Block" entity type.
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'block_content'
+'node'

That means that this is ready for review. If you want to test this manually you can follow the steps in the first comment after the issue summary.

@amitaibu
Copy link
Owner

Awesome! I'm finally going to have some OG time this coming week(s), so I plan to review this properly.

@@ -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
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
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

@amitaibu
Copy link
Owner

I've added a minor comment, but this is good to go. Thanks!

@amitaibu amitaibu merged commit a929694 into amitaibu:8.x-1.x Mar 31, 2016
@pfrenssen
Copy link
Collaborator

Awesome, thanks!

amitaibu added a commit that referenced this pull request Sep 20, 2016
@pfrenssen pfrenssen deleted the OgComplexWidget-target_type branch September 22, 2017 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants