Currently Measured Value Field doesn't support EntityMetadataWrapper. It would be really nice to have an ability to use this field in wrapper.

How to reproduce:
Add mvf field to the node, check available wrapper properties:

$node = node_load(1);
$wrapper = entity_metadata_wrapper('node', $node);
dsm($wrapper->getPropertyInfo());

added mvf field not available for using.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

skorzh’s picture

Status: Active » Needs review
FileSize
1.59 KB

My patch fixed it.
Now you can work with mvf field in wrapper.

Some examples:

$node = node_load(1);
$wrapper = entity_metadata_wrapper('node', $node);

// Get unit value:
$value = $wrapper->field_mvf->value->value();
dpm($value); // 12

// Get Measure id:
$unit_id = $wrapper->field_mvf->unit->raw();
dpm($unit_id); // 5

// Get whole unit object:
$unit_object = $wrapper->field_mvf->unit->value();
dpm($unit_object); // Unit Object

// Get Measure label:
$unit_label = $wrapper->field_mvf->unit->value()->label;
dpm($unit_label); // Dollar

// Change field value and measure type:
$wrapper->field_mvf->value = 5;
$wrapper->field_mvf->unit = 6;

$value = $wrapper->field_mvf->value->value();
dpm($value); // 5

$unit_label = $wrapper->field_mvf->unit->value()->label;
dpm($unit_label); // Euro

// For multiple fields:
$value = $wrapper->field_mvf[0]->value->value();
dpm($value); // 5

// Check all multiple values:
foreach($wrapper->field_mvf as $field) {
   $value = field->value->value();
   dpm($value); // 5
}
AndrewsizZ’s picture

Nice, tested on ERPAL, works like a charm!

Spleshka’s picture

Status: Needs review » Reviewed & tested by the community

For me it also looks good, thanks for this patch.

skorzh’s picture

No updates here?

skorzh’s picture

Still no updates here?

Spleshka’s picture

Well, I would also like to see this patch commited.

Greg Varga’s picture

#1 works perfectly.

Additional info on setting units and values:

// Set field value and measure type for the _first_ time:
$wrapper->field_mvf->set(array('value'=>2, 'target_id'=>35));

// Load the unit by it's machine name using:
$unit_entity = units_unit_machine_name_load([unit machine name]);

// Get Unit Id:
$unit_id = $unit_entity->umid;
skorzh’s picture

Still no updates here?

skorzh’s picture

More then one year no any response from maintainers, maybe it make sense to at least change status from 'Actively maintained' and don't confuse people?

bucefal91’s picture

Hello!

This is me, whom you are blaming. And you do so rightly. I dedicate to this module less time than I would like to. I maintain other 3 modules on d.org and this one has the least popularity, so I feel obliged to give it only a fraction of my time. But I am willing to sort this issue.

Thank you for the patch. I've reviewed it - you got most of it done. The only suggestion I could come up with is that it would be great to also include into the wrapper API ability to fetch MVF value in a specific unit. Right now using the patch we can fetch both absolute value and the unit that corresponds to that value, but we're basically forced to do the conversion in client code, which is not that nice.

So, let's get one more feature:

$r = $wrapper->field_mvf->value->value(array('unit' => 6));
// Now $r contains field_mvf value converted into EUR. Instead of '6' we could also supply fully loaded unit entity or its machine name.

Or it could be:

$r = $wrapper->field_mvf->value(array('unit' => 6));
// Now $r is array as following:
$r = array(
  'value' => 789, // whatever it is in EUR,
  'target_id' => 6, // target_id should hold the unit ID into which it was converted
);

I've tried to implement this addition tonight, but entity API wrappers abstractions are definitely way too baked for my brain after midnight. I'll get it tomorrow.

In the mean time I only edited 2 lines of code: strictly speaking both 'value' and 'unit' need 'schema field' and we map from 'value' and 'unit' properties to specific columns through mvf_subfield_to_column() function.

bucefal91’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
3.43 KB
4.07 KB

I am glad I heard the voice of rationalism in my head and put it off yesterday. No way I would have gotten it done last night. After poking entity API module seems like the best option I can get is:

$r = $wrapper->field_mvf->converted_euro->value();
// Now $r holds the value of MVF field in EUR.

So it takes the form of

$wrapper->field_mvf->converted_{machine_name_of_the_target_unit}->value();

Apart from it nothing is changed. I enclose the 2 patches: against current 7.x-1.x and interdiff against korgik's contribution. Let's see if you like it?

skorzh’s picture

Status: Needs review » Reviewed & tested by the community

Sorry, did't want to blaming you :) Just wanted to pay your attention on forgotten issue.
I've tested your patch, works good for me, its definitely a good feature to get converted value, but maybe we have to documented it somewhere since its not obvious how to use it?

  • bucefal91 committed c713852 on
    Issue #2405779 by korgik: Adding entity metadata wrappers support.
    
  • bucefal91 committed fb050e9 on
    Issue #2405779 by korgik, bucefal91: Extending support for MVF metadata...
bucefal91’s picture

Status: Reviewed & tested by the community » Fixed
FileSize
1.8 KB

Good point. I've added our PHP snippets from this issue into README.txt (patch enclosed). I also have a rule of thumb of not publishing stable release of any module without up-to-date existing documentation page. So, when MVF gets stable for 7.x-1.0 release, I'll include this info into its documentation page too.

skorzh’s picture

Great, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

elpino’s picture

Is is possible to get the converted value's unit label or symbol?

bucefal91’s picture

Not really, since you are the one who dictated into what unit to covert, it is assumed you already know its label or unit or anything else about the target unit. I'd do something like the following (load the unit, so later on we can fetch its label, and get MVF converted into it):

$target_unit = units_unit_machine_name_load('euro');
$property = 'converted_' . $target_unit->machine_name;
$r = $wrapper->field_mvf->$property->value();

echo $r . ' ' . check_plain($target_unit->label()); // Outputs "100 Euro".