0

Struggling to find the answer to this. I have an entity called "school objects" which has a bundle called "homepage". One of the fields attached to this is called "Hero banners" and it allows multiple text values.

Example

All I want to do is print each value. I don't need to check for conditions, I don't need NIDs, I just want each value. I am not really a php developer but understand the basics of what it is doing. I was looking into the EntityFieldQuery code, but cannot get anything to print on a tpl file I am using.

The output I need, based on image attached, is simply:

<div class="value_1">images/symbol.png</div>
<div class="value_2">Let's make a difference</div>
<div class="value_3">Start today</div>

All I can find after a lot of searching is complex bits of code checking for conditions and stuff that I do not need. I need some kind of array I think so that my template looks like:

<div class="value_1">Value[0]</div>
<div class="value_2">Value[1]</div>
<div class="value_3">Value[2]</div>

All I got so far is this:

query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'school_objects')
  ->entityCondition('bundle', 'homepage')
  ->range(0, 14);
$result = $query->execute();

I have read the link to this on the Drupal site but cannot understand it or how to print what I need.

2 Answers 2

0

Try the following and load the actual entity to get its field values.

The code assumes you have drupals entityTypeManager injected via dependency injection. Otherwise use \Drupal::entityTypeManager() isntead of $this->entityTypeManager()

// Get the entity ids.
$entityIds = $this->entityTypeManager()->getStorage('school_objects')->getQuery()
  ->condition('type', 'homepage')
  // Add more conditions if you need.
  ->execute();
// I assume only one id is returned and reset the array. Otherwise go over 
// each Id with a foreach() and do this in a loop.
$entityId = reset($entityIds);
$schoolObject = $this->entityTypeManager()->getStorage('school_objects')->load($entityId);
// Now get the values of your field. I assume the fields machine_name is 'field_hero_banners'. Adjust if needed.
$values = $schoolObject->get('field_hero_banners')->getValue()
// Loop over all values.
foreach ($values as $item) {
  $value = $item['value'];
}
6
  • This just breaks the page. Also this bit "\Drupal::entityTypeManager()" I tried instead of the other line and that also breaks the page. Do I add most of this code to the top of the page, then the loop bit between my html tags? I am total novice with php. Commented Mar 5, 2019 at 8:54
  • well, this code should be part of a Controller class or something similar. In a drupal way I'd add this code to a controller, prepare the array of values for a twig template, add a custom twig template with a loop to print those values between your html tags. You can't just put this code in a html file. That means this is part of a custom module. I could help you with that, but I doubt this would help you, given your little knowledge of php/drupal?
    – theuni
    Commented Mar 5, 2019 at 8:59
  • No idea what a controller class or a twig is. Was trying to adapt some existing php that is on a tpl file. But seems I am years away from that at the moment. Thanks for your help, this might help someone on a higher level than me. Commented Mar 5, 2019 at 9:15
  • I think the sample of code in the question for drupal 7, and your code snippet for drupal 8, @Rich Stevens which version of drupal you use ?
    – berramou
    Commented Mar 5, 2019 at 10:53
  • Drupal 7.I also made it this far now: <?php $query=new EntityFieldQuery(); $query->entityCondition('entity_type', 'school_objects') ->entityCondition('bundle', 'homepage'); $result=$query->execute(); if (!empty($result['school_objects'])) { $nids=array_keys($result['school_objects']); $entity_testimonails=entity_load('school_objects', $nids); foreach($entity_testimonails as $testimonails) { $wrapper=entity_metadata_wrapper('school_objects', $testimonails->id, array('bundle' => 'homepage'));}}?> <!--html here--> <h1><?php echo ($wrapper->field_hero_banners->value());?></h1> . Commented Mar 5, 2019 at 11:44
0
<?php
$wrapper = entity_metadata_wrapper('school_objects', '353', array('bundle' => 'homepage'));
$homepage_hero_banners = $wrapper->field_hero_banners->value();
?>
<p><?php echo $homepage_hero_banners[1]; ?></p>
<p><?php echo $homepage_hero_banners[2]; ?></p>

It has taken 4 days for me to get this code. I truly hope this helps someone else!

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.