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

mo86’s picture

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

ksavoie’s picture

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.

drupalfan2’s picture

Did you find a solution for manipulating field in the .theme file?