1

I want to get access to a special field value, do some processing or manipulating on it and then place it back in theme. For example, I've created a custom content type that name is "News" and has a field with below properties:

  • label: News Number
  • machine name: news_num
  • value: Number (float)

I did content-type node theming with name node--news.html.twig, Now I want to understand Is there any preproccessing hook to modify or altering the field value and in addition I want to learn right way for manipulate the field value, I wrote something like below code:

ThemeName.theme:

function ThemeName_preprocess_node(&$variables) {
// Get access to field value:
  $value = $variables['node']->news_num();
/**
* Makeing some manipulation on $value here.
*/
  $variables['value'] = $value;
}
  1. The type of operation is not really important; I'm most looking for a solution to learn standard method and general format to access and modify the field value.
  2. I just want to overview the field value (alter the field value before rendering) and not doing any override or change the field value in the database.

Any suggestions?

  • Comments have been removed. Please don't use them for extended discussion. – kiamlaluno Aug 29 '16 at 10:20
2

I suggest you two solution one based on what you tried to did

First solution

in yourthemename.theme

if ($variables['node']->getType() == 'YOUR_CONTENT_TYPE') {
    $variables['My_Field'] = $variables['node']->get('YOUR_FIELD')->value;
  }

and in your custom node template

   {{ My_Field }}

Tips: Those of "YOUR_CONTENT_TYPE" and "YOUR_FIELD" are machine names for your content type and your content field.

Second solution

in your content type node template file ( I mean if your content type name is News in node--news.html.twig) try to add your custom value to it with:

  {% set My_Field = node.YOUR_FIELD.value
      {# Make your manipulation here #}
   %}
   {# And then print it back wherever you like with below code: #}
  {{ My_Field }}
  • I agree, this is good solution for what he/she looking for. second solution is better – David Mcsmith Aug 29 '16 at 4:59
  • @zhilevan: Thank you so much for your attention, and special Thank's again for your time that grant to me, but submitted answer not work for me and seems that's should be for drupal 7 – Mojtaba Reyhani Aug 29 '16 at 5:26
  • @MojtabaReyhani you're welcome, no, this is Drupal 8 solution, I used something like this several times in my Drupal 8 projects. ( {{ and {% are twig tags that used in drupal 8 ). anyway tell me more about what did you try, what is your template files name you put this codes there? are you sure your template node file getting render? are you sure rebuild the cache after change? – Yusef Aug 29 '16 at 5:30
  • @zhilevan: Thanks, My problem solved by changing a bit to your code, I try to create a general format solution, With your permission I want to edit your answer and make it a Slightly better.Would you allow me? – Mojtaba Reyhani Aug 29 '16 at 5:37

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.