0

I have a field and I can render its value in a such way:

<?php print $entity1->field_list_of_options['und'][0]['safe_value'];?>

But I have many different entities at the page so I want to hide the following tail in a function in order to get something like this:

<?php print $entity1->Get_list(); ?>

How can I do it? I tried to get predefined functions in a custom module but couldn't find a proper hook. How can I handle this problem?

I wrote the following in order to collect my "tails":

function mymodulename_getText($entityId,$field){ return $entityId->$field."['und'][0]['safe_value']"; }

And so, I use it in such way:

<?php $enityId = 2; $entity2 = entity_load_single('entitytype', $entityId); ?> <p><?php print getText($entity2, field_list_of_options); ?></p>

But I don't know how to implement my function to my custom module in order to get it in node's body where I want to print it using PHP Filter.

6
  • Is there a reason you want to add a method to your $entity1 object. as opposed to some general purpose function (e.g. mytheme_get_list($entity1))?
    – Shawn Conn
    Jun 14, 2016 at 0:20
  • @ShawnConn, The reason is following: I use ECK module in order to create custom entities, so, I'am afraid, I wouldn't be able to get my entities in a proper way by using this function. I get the entity array by the function entity_load_single($type, $id) and then render proper fields and their values from it. At the end, I have 5 different entities at the same page, so I decided to work this way. What can you advise me?
    – drru
    Jun 14, 2016 at 8:45
  • If the goal here is solely rendering the entity data field data, you might better off defining a template function that renders the markup and injects it into template variables. I don't know your entire use case so maybe extending the entity object with your own custom method makes sense.
    – Shawn Conn
    Jun 14, 2016 at 22:10
  • @ShawnConn, could you please explain this approach more clearly, I don't get an idea...
    – drru
    Jun 15, 2016 at 12:28
  • All I'm suggesting is it might be easier to add a template function to your theme to iterate/print over a multi-fielded entity rather than extending the entity with your own custom method.
    – Shawn Conn
    Jun 17, 2016 at 2:41

1 Answer 1

0

The correct syntax for your approach would be:

function mymodulename_getText($entityId,$field){
    return $entityId->$field['und'][0]['safe_value'];
}

I suggest to sanitize that with a isset().

4
  • I get the following warnings: Warning: Illegal string offset 'und' in mypages_getText() (line 113 of /sites/all/modules/mypages/mypages.module). Warning: Illegal string offset 'safe_value' in mypages_getText() (line 113 of /sites/all/modules/mypages/mypages.module). Notice: Trying to get property of non-object in pages_getText() (line 113 of /sites/all/modules/mypages/mypages.module).
    – drru
    Jun 21, 2016 at 12:25
  • So I have the following code: function mypages_getText($entityId, $select_field){ return $entityId->$select_field['und'][0]['safe_value']; } and I call this function in a such way: if (module_exists('mypages')) {print mypages_getText('2', 'field_test_field'); } what do I do wrong?
    – drru
    Jun 21, 2016 at 12:29
  • If you call your func with '2' as $entityId, you need to load the real entity object, '2' is just a string ("non-object")! I.e. with node_load(array("nid" => $entity_id)). Jun 21, 2016 at 13:27
  • is it impossible to do with entity_load_single()?
    – drru
    Jun 21, 2016 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.