Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ar-jan’s picture

Issue summary: View changes

Would this also address using (entity?) tokens or should that be a separate issue? Currently if you use a multifield's token (e.g. with auto_entitylabel) you get the serialized data.

byung’s picture

Thanks Dave for picking this back up. Search indexing would be a bigger help than views issue for me.

mihai_brb’s picture

I was playing with multifield and entity property info. After adding the multifield fields as multifield type the sub-fields are availabe in Search API. However the values are not picked up, and therefor not indexed. I guess this is because the sub-fields require custom getters? Or how would one quickly implement this?

Thanks,
Mihai

Dave Reid’s picture

e0ipso’s picture

Status: Active » Needs review
FileSize
69 bytes

This will allow you to do something like:

$wrapper = entity_metadata_wrapper('node', $node);
echo $wrapper->field_multi_name->field_first->value();

This patch only adds get support. So feel free to review it and set it to Needs work for set support.

deviantintegral’s picture

"Set the property type based on the targe type."

Missing t on target.

The patch needs to be rerolled with --relative.

I was *sure* that there was a way to do this within a class instead of a function callback (like EntityDefaultMetadataController). If so, that's a better way to go since it means code is only loaded if it's needed. But, I can't find it now, so perhaps it does have to be in the module directly.

e0ipso’s picture

FileSize
2.73 KB

Argh, I always forgot about the --relative.

Rerolled.

e0ipso’s picture

FileSize
2.73 KB
309 bytes

This update addresses a weird cache issue.

Dave Reid’s picture

+++ b/multifield.module
@@ -131,6 +132,14 @@ function multifield_entity_info() {
+function multifield_access_callback($op, $entity, $account, $entity_type) {
+  // For now grant access to anyone asking for multifield entities.
+  return TRUE;
+}

Should this run field_access() instead?

e0ipso’s picture

FileSize
3.55 KB
1.24 KB

@Dave Reid do you mean something like this?

e0ipso’s picture

Catch the case when there is no entity id.

e0ipso’s picture

e0ipso’s picture

FileSize
3.71 KB

Updated patch again.

becw’s picture

I'm using this with Search API, and I'm finding that some multifield subfields are not available to add to the Search API index. When I index my content, I get errors like:

Illegal offset type in isset or empty entity.wrapper.inc:1036 [warning]
array_flip(): Can only flip STRING and INTEGER values! MultifieldEntityController.php:16 [warning]
array_flip(): Can only flip STRING and INTEGER values! MultifieldEntityController.php:16 [warning]
array_flip(): Can only flip STRING and INTEGER values! entity.inc:350 [warning]
array_flip(): Can only flip STRING and INTEGER values! entity.inc:350 [warning]

I'll look into this and try to provide some more details and a fix, but it might be a week or two before I can work on this again.

Jelle_S’s picture

Patch provides full entity property API integration. Should also fix the errors mentioned above.

becw’s picture

It turns out that my array_flip() errors were not coming from multifield itself, and the subfields are available, they're just hard to find (they're nested a few layers deep). Both patch 13 and patch 15 work for me in that they make multifield data available to Search API indexes. The significance of the differences between the two patches is not clear.

jOksanen’s picture

Status: Needs review » Reviewed & tested by the community

Patch 15 tested and works for me as it should. Was able to use it as a facet as well.

Marking as RTBC.

odegard’s picture

Haven't looked at the code, but can confirm that it works as intended. I'm using it in a module where I iterate over subfields to get values.

mkinnan’s picture

I reported on an issue here:
https://www.drupal.org/node/2392713#comment-9551519

that appears to be related to the multifield_access_callback function from patch #15. Unfortunately, I don't know enough about node access in Drupal to be able to troubleshoot

edmund.kwok’s picture

+1 with #15. Able to iterate perfectly with unlimited values multifield.

I can't get the setter working though. Anyone has any ideas on the syntax for set to work? Tried a few permutations of this but still no joy.

  $wrapper->field_category[0]->field_score = 8.5;
  $wrapper->save();

  $value = $wrapper->field_category->value();
  $value[0]->field_score[LANGUAGE_NONE][0]['value'] = 8.5;
  $wrapper->field_category = $value;
  $wrapper->save();
dobe’s picture

I am with #20. Cannot get these values to save programmatically.

dobe’s picture

Status: Reviewed & tested by the community » Needs work
dobe’s picture

Here is a patch that removed some things from the setter callback. I could not make heads or tails as to why it was there. But after I removed it.

Then

<?php
 $field_category_score = array(
   'field_category' => array(LANGUAGE_NONE => array(array('tid' => 10))),
   'field_score' => array(LANGUAGE_NONE => array(array('value' => 8.5))),
 );

  // This would work;
 $wrapper->field_category_score->set(array($field_category_score));

  // This would NOT work;
 $wrapper->field_category_score[] = $field_category_score;
?>
dobe’s picture

Status: Needs work » Needs review
kopeboy’s picture

(not a developer)
Does this mean that we cannot set subfield values with Rules?
..or should I open a new issue?

mparker17’s picture

The patch in #23 worked for me: I can now interact with multifields using entity metadata wrappers.

Andrej Galuf’s picture

We have multiple multifields on the node. For some reason, on some nodes the entity wrapper pulls completely wrong multifield data.

Accessing it traditionally returns the real value:

$start = $node->field_sales_season[$l][$d]['field_starting_month'][$l][$d]['value'];

$start returns 3.

Accessing it through wrapper, though:

$test = $wrapper->field_sales_season->value();
$testToo = $wrapper->field_target_price_range->value();

$test === $testToo

We have 8 different multifields, of which these two are the only ones on this node bundle, so I am assuming the wrapper has problems distinguishing between various multifields. Note that this only happened on those nodes that had values in the second multifield and that when I deleted / re-added the field, the bug went away.

filip_p’s picture

Any news on implementing this in an update to Multifield? I could REALLY use this feature but I'm not familiar with patching.

Ada Hernandez’s picture

Status: Needs review » Reviewed & tested by the community

#23 seems to work.

ademarco’s picture

#23 works for me too.

bkat’s picture

I'm trying to use editablefields module on multifield subfields to allow editing of elements on the parent nodes view page or within views.

I have things pretty much working except that then editablefields calls entity_save() nothing actually gets written to the database.

Should this patch allow us to save a multifield entity outside of saving the host entity?

entity_save is a no op because we don't have a save method on the entity, no "save callback" is defined for the entity info and MultifieldEntityController does not implement EntityAPIControllerInterface.

As an experiment, I tried changing the controller to extend EntityAPIController but that fails because the base table is "multifield" and what we really want to write to is field_data_field_$multifield.

If this is out of scope for this patch, I'll submit a new issue.

Sebastien M.’s picture

works for us too.
could you generate a new release with, at least, this fix ?
many thanks for the work

vasike’s picture

I can confirm RTBC.

vasike’s picture

i used the solution here for a Multifield integration with Search API attachments : #2670994: Files attached to multifield fields are not indexed

Derimagia’s picture

Status: Reviewed & tested by the community » Needs work

This was only implemented for the field itself, for sub-fields this needs work.

$wrapper->field_multifield->field_textfield->value(); and $wrapper->field_multifield->field_textfield->set('Test') should work

Kris77’s picture

It would be very useful to loop through multifield values and carry out actions with the subfields in Rules module.

MustangGB’s picture

Ran into I believe the same problem as #35.

Essentially I'm trying to get Multifield to work with Services Entity API

The JSON I'm sending is something like this:

{
	"title": "This is my entity",
	"type": "page",
	"multifield": [
		{
			"subfield0": "123",
			"subfield1": "1234",
			"subfield2": "12345"
		},
		{
			"subfield0": "234",
			"subfield1": "2345",
			"subfield2": "23456"
		}
	]
}

And these are the changes to #23 I made to get it working, I didn't bother to roll a patch though as probably something needs to be done to remove the hardcoded ['value']:

   function multifield_field_property_set($entity, $name, $value, $langcode, $entity_type, $info) {
     ...
-    $entity->{$name}[$langcode] = $values;
+    $items = array();
+    foreach ($values as $delta => $data) {
+      foreach ($data as $field_name => $value) {
+        if (is_array($value)) {
+          $items[$delta][$field_name][$langcode][] = $value;
+        }
+        else {
+          $items[$delta][$field_name][$langcode][]['value'] = $value;
+        }
+      }
+    }
+    $entity->{$name}[$langcode] = $items;
     ...
   }
abarrios’s picture

In order to complete this patch, the "MultifieldEntityController" class should implement "EntityAPIControllerInterface" from the entity module. This patch doesn't implement that, it just extends from core "DrupalDefaultEntityController".

The current status reports errors in different parts of a normal workflow with entity module enabled because this is not implemented and it should be because that is declared in the hook_entity_info().

Some error samples that you might encounter using this patch:

 WD php: Warning: class_implements(): Class MultifieldEntityController  does not exist and could not be loaded in entity_entity_info_alter()
 (line 1474 of /../docroot/profiles/dss_magdalena/modules/contrib/entity/entity.module).
 WD php: Warning: in_array() expects parameter 2 to be array, boolean given in entity_entity_info_alter() (line 1474 of /../docroot/profiles/dss_magdalena/modules/contrib/entity/entity.module).
mvdve’s picture

I have an issue where the entity translation is not loaded. Only the original language is selected within the getter callback. The result is that the wrong row/id will be loaded (the one from the original language).

I updated the patch but it replicates the fallback mechanism of the entity_translation module which doesn't look like the right way to handle this.
It would be logical for the getLanguage() method to return the current language but is selects the original language.

Please let me now when there is a nicer way in handling this. After this has been resolved, i will implement the EntityAPIControllerInterface interface to complete the patch.

mvdve’s picture

Improved patch. The language property should be used when it is set. For example when the entity is created with migrate/feeds.

Status: Needs review » Needs work

The last submitted patch, 40: 2041531-40-entity-api-support.patch, failed testing.