0

I have a content type in Drupal 8 project that has image, name, info and body field as a default display, and image,name and information as a teaser display. I am able to display this content type with teaser view at the page, but right under that i would like to get the default display along with body field so i can create a bootstrap modal with that.

I am using hook_node_view in the module level to change the #view_mode of that render array to 'full' instead of 'teaser'.

function team_bio_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {

if($entity->bundle() == 'about_us'){
 if($view_mode == 'full'){
  if($display->getComponent('field_team_bio')){
    $build['full_view'] =  $build['field_team_bio'];
     $build['full_view'][] = array(
       'full_view' =>array(
         '0'=>array(
            '#view_mode'=>'full'
         ),
         '#weight'=>'103'
       )
    );
    kint($build);

  }
 }
}
}

i can display the content type right under teaser views, but i can't change the view_mode to full. This is what i see when i kint($build):

enter image description here

Current contents have name like '0','1','2','3','4', and they have the view mode that are 'teaser' but i dont how to pass 'view_mode' to 'full'in render array, I am not sure if there is specific name for that or not. Trying this:

     $build['full_view'][] = array(
       'full_view' =>array(
         '0'=>array(
            '#view_mode'=>'full'
         )

didnt change the view_mode neither. So my question: What is the best way to change these view modes to full for the contents? Should I search these numeric keys in the array and pass them to another array then change view_mode like that? Or is there an easier way to approach the problem?

  • You are putting #view_mode in $build['full_view'][], which is an array with nothing to render; supposing it worked, Drupal would not render anything. Did you try setting #view_mode in $build['field_team_bio']? – kiamlaluno Jul 14 '16 at 17:07
  • #view_mode is already set to teaser in $build['field_team_bio']. I am trying to copy all these values and copy them into $build['full_view'] so i can display content twice in the page. Then the copy array has arrays name '0','1','2','3','4' that their #view_mode to be change to full. so I will have teaser view, and full view of same content in the same page, so i can pass full view into bootstrap modal. Sorry if my question was vague :) – Mr.T Jul 14 '16 at 17:25
  • So, why are you trying to change the view mode to full when it is already that the view mode? Entity fields don't have a separate view mode; they use the same view mode used from the entity. – kiamlaluno Jul 14 '16 at 17:39
  • #view_mode on the field level is just an information for templates and other places to check that. It is not used again, that's too late. The render array that you are seeing has already been built based on the view mode. – Berdir Jul 14 '16 at 17:42
  • I actually solved the problem. Thank you for your time. I will post the solution now. – Mr.T Jul 14 '16 at 18:10
0
function team_bio_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
  if($entity->bundle() == 'about_us'){
    if($view_mode == 'full'){
     if($display->getComponent('field_team_bio')){

       $team_bios_full = [
        '#theme' => ['team_bio_full_wrapper'],
        '#weight' => $build['field_team_bio']['#weight'] + 1,
       ];

    foreach(Element::children($build['field_team_bio']) as $delta){
      $team_bio_full = $build['field_team_bio'][$delta];
      $team_bio_full['#view_mode'] = 'default';

      $team_bios_full['#team_bios'][] = $team_bio_full;
    }

    $build['team_bios_full'] = $team_bios_full;
  }
 }
}
}

Then I passed team-bios-full-wrapper into class and render it.Like this:

{%
  set classes = [
  'team-bios-full-wrapper',
  ]
%}
<div{{ attributes.addClass(classes) }}>{{ team_bios }}</div>

Sorry i have different variables, i just rethought and rewrote the code. Hope this helps out someone.

  • Thanks! I had to implement this in a module instead of the theme using hook_node_view_alter() to work. In the theme neither hook_node_view() nor hook_preprocess_node() worked; all adjustments to the media image (entity reference) were ignored / reset. – sun Mar 21 '18 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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