Is this not yet ready in Drupal 7?

I have a custom action that should change the size of images using a field value, to scale them.

The custom action works, but the $node object is not there at all, as I even tried to resize the images using $node->nid (which should be an integer). The last idea was to do a query with the nid but of course it would be much better to have the value there.

I'm asking also because we still see

If possible, the owning $node object may also be available.

when you edit the custom action... maybe a D6 thing? Hopefully not...

Thanks!

Comments

ferrangil’s picture

Status: Active » Fixed

I can access the data from "cck" fields doing this:

foreach ($fields as $fid => $field_instance) {
	foreach ($field_instance as $field_id => $entity_types) {
		foreach ($entity_types as $entity_type => $entity_instances) {
			foreach ($entity_instances as $entity_id => $entity_stub) {
				$entity = entity_load_single($entity_type, $entity_id);
				$scala_plate = $entity->field_figure_escala_plate[LANGUAGE_NONE][0]['value'];
			}
		}
	}
}

Not as easy as doing $node->my_field but works :)

Status: Fixed » Closed (fixed)

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

d0t15t’s picture

so you include that in your image cache action?

denjell’s picture

Category: bug » feature
Status: Closed (fixed) » Active

I guess this shouldn't have closed. I would like an answer to this as well. Anyone with some positive experience in this regard?

fietserwin’s picture

Assigned: Unassigned » fietserwin

If you are processing an image

- it may be a managed image file (in the database identified by a fid)'.
(=> if not, there will not be fields nor nodes.)
(the file name field is unique as well, thus there wont be multiple managed file records referring to it.)
- If it is a managed file there will be 0, 1 or more fields referring to the managed file.
- A field belongs to exactly 1entity.
- Thus there will be 0, 1, or more entities referring to the image.
- A node is an entity, an entity is not necessarily a node.
- Thus expecting a $node variable denies the D7 relational model behind images, managed files, fields and entities.

So I guess you may expect a managed file object, a set of fields and a set of entities. Currently you will only find the set of fields in the variable $fields (along some iamge properties like $width and $height). Documentation can be improved though.

Also note that the text effect (issue #1090312: Add text effect to D7) does more or less the same but does give you a $entity, This because often times there will only be 1 field and thus 1 entity be referring to the image. For these cases the $entity variable comes in handy. I will look into reusing that code with this effect.

mattbk’s picture

Is the code for getting the node title similar? Would one just replace field_figure_escala_plate with title or is there a Drupal way to do this?

fietserwin’s picture

No, title is not a field in D7 but a property of a node. So if you know that the entity will be of type ' node', You can use $entity->title.

dman’s picture

That's a good explanation. In short, we often don't know where supporting data may come from, so it tries a few places and *may* come back with structured data which may be a variety of shapes :-)

Without the textactions around to really utilize the data extracted from the context - there has not been a lot of test cases for this particular feature in the upgrade. If text processing comes back on line, then this sort of data extraction will become a lot clearer.

The real pain is that you can't 'test' it on the sample image because the sample image has no node. So it requires intimate knowledge of internal structures (or lotsa debug logging) or a lot of guesswork to find a result. I can't suggest how that can be improved.

fietserwin’s picture

Assigned: fietserwin » Unassigned
Status: Active » Fixed

The code has been adapted to become more in line with the text effect that will be added as per #1090312: Add text effect to D7.

Changes:

  • internal: field renamed from text to php: run update.php to change this on existing styles.
  • snippet should return true/false to indicate success/failure.
  • $image now contains the image and its info.
  • $image_context contains context about the image like 'managed_file' and 'entities'.
  • extensive documentation added in README.txt.

This probably means that you will have to manually update all your current custom actions.

denjell’s picture

Hey thanks. I am going to give it a try right now - and will report back.

Status: Fixed » Closed (fixed)

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

attheshow’s picture

I was able to pull in the text information I needed to get the text from $node->title and another custom field using the following snippets:

foreach ($image_context['referring_entities']['field_story_home_image']['node'] as $nid => $node) {
  $title = $node->title;
}
return $title;
foreach ($image_context['referring_entities']['field_story_home_image']['node'] as $nid => $node) {
  $other_field_value = $node->field_story_home_image_title1['und'][0]['value'];
}
return $other_field_value;

If you have trouble figuring out what the exact array elements you need are, I recommend turning on the Devel module (so that you'll have access to the dsm() function) and then going temporarily into the image_effects_text.inc file and adding the line:
dsm($image_context);
Into the image_effects_text_get_text() function. This will print out the contents of the $image_context array for you so that you can take a look at what you have to work with.

fietserwin’s picture

Or you just look at the README.txt of the text effect module and see that this has already been done for you with some example data... As you seem to fetch only 1 title (of a node) I would like to suggest you to use (the easier and multilingual safe):

$title = $image_context['entity']->title;
$field = field_get_items('node', $image_context['entity'], 'field_name');
$other_value = $field[0]['value'];
fietserwin’s picture

Issue summary: View changes

Edit to include more information.