Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By ksavoie on
My head is stuck in D7, can someone assist in how I need to manipulate field values in preprocessing for D8?
Here is where I am now. It semi-works as in I am getting at the field value I need to drop into a var for twig, but I cant figure out how to remove/unset the field so it wont render.
Also I am fairly sure there this is the wrong (long) way of manipulating field values (can someone put me on the right path?) Any assistance greatly appreciated!!!!
function myTheme_preprocess_block(&$variables) {
$viewMachineName = $variables['elements']['content']['#display_id'];
if ($viewMachineName == 'block_my_view_machineName') {
// get node field
$sidebarTitleField = $variables['elements']['content']['#view']->result[0]->_entity->get('field_sidebar_callout_title')->getValue();
// set var to be used in twig (would rather update object so this field value drops into the blocks title if I knew how)
$variables['blockLabel'] = $sidebarTitleField [0]['value'];
// remove field so it wont render (but doesn't work)
$variables['elements']['content']['#view']->result[0]->_entity->__unset('field_sidebar_callout_title');
}
}
Comments
Just go to /admin/structure
Just go to /admin/structure/types/manage/YOURCONTENTTYPE/display and disable the fields you dont want to show :) YOu can configure all fields for each "display mode" like "teaser" etc.
You can also create new display modes in admin/structure/display-modes/view
Hope this helps
Thanks for the reply mo86,
Thanks for the reply mo86, however ultimately I am in need of more finite control of output. This was a simplified example in order to get my head in the right direction. While I understand and could use your method to solve this specific application, the understanding of how to programmatically manipulate field values in preprocessing is what I am after.
Did you find a solution for
Did you find a solution for manipulating field in the .theme file?