3

i add to my main menu an image field programmatically for build a megaMenu :

/**
 * Implements hook_entity_base_field_info().
 */
function hook_entity_base_field_info(EntityTypeInterface $entity_type) {

    $fields = [];


    if ($entity_type->id() == 'menu_link_content') {


        $year = date('Y');
        $month = date('m');

        $fields['image'] = BaseFieldDefinition::create('image')
            ->setLabel(t('Image'))
            ->setDescription(t('Image field'))
            ->setSettings([
                'file_directory' => 'imagesMenu'. '/' . $year . '/' . $month,
                'alt_field_required' => FALSE,
                'file_extensions' => 'png jpg jpeg',
            ])
            ->setDisplayOptions('view', array(
                'label' => 'hidden',
                'type' => 'default',
                'weight' => 0,
            ))
            ->setDisplayOptions('form', array(
                'label' => 'hidden',
                'type' => 'image_image',
                'weight' => 0,
            ))
            ->setDisplayConfigurable('form', TRUE)
            ->setDisplayConfigurable('view', TRUE);
    }

    return $fields;
}

After a drush update-entity, i can see the field on my form and i can add the image.

Now i try to get this field into twig.

When i use kint with keys this is the result :

menu--main.html.twig

enter image description here

I cannot see my field image.

Any idea how can i get my field in tempalte ? hook_preprocess for the menu ?

4

This is how i get my custom image field from menu into twig

function hook_preprocess_menu(&$variables, $hook) {

    if($variables['theme_hook_original'] == 'menu__main'){

            $items = $variables['items'];
            foreach ($items as $key => $item) {

                if(count($item['below'])>0)
                {
                    foreach($item['below'] as $id=>$array)
                    {
                        $entity = \Drupal::entityTypeManager()->getStorage('menu_link_content')->loadByProperties(array('uuid'=>$array['original_link']->getDerivativeId()));

                        foreach($entity as $idMenu)
                        {


                            if($idMenu->get('image')->entity != null) {


                                $image = file_create_url($idMenu->get('image')->entity->getFileUri());

                                $items[$key]['below'][$id]['imageUrl'] = $image;


                            }
                        }

                    }

                }

            }

        $variables['items']=$items;

    }

}

I just work on my item menu and add 'imageUrl' into my array. I first load my entity by properties with the UUID for access to my menu item fields.

This method is only for my sub menu ( inside my mega menu, i want just add an image by item in submenu )

that why i check count(item['below']>0)

enter image description here

For now i can't find better way.

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.