Preprocess function views_fields

Descripción
Descripción

Las funciones de preproceso son la manera "tradicional" en que podremos pasar valores dentro de cualquier contenido de drupal, aunque hoy en día ya se utilizan módulos como Twig Tweak, para realizar esta clase de procedimientos.

Instrucciones
Instrucciones

En el archivo .theme de tu tema personalizado, añade el código sustituyendo el valor por el nombre de tu tema en la función.

Código
MY_CUSTOM.THEME

/**
 * passing variables to view template
 *
 * @param $variables
 *
 * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
 * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
 */
function TUTEMA_preprocess_views_view_fields(&$variables) {
  if (isset($variables['view'])) {
    $view = $variables['view'];

    if ($view->id() == "pending_reviews" && $view->current_display == "page_1") {
      $result = $view->result;

      /**
       * @author or Logged detector
       */

      // Load the current user.
      $user = \Drupal\user\Entity\User::load(Drupal::currentUser()->id());
      $variables['gem_user'] = $user->id();


      //Custom Greetings Messages
      // $variables['greetings_user'] = $user->get('field_custom_greetings_message');
      $variables['greetings_user_0'] = $user->get('field_custom_greetings_message')[0]->value;
      $variables['greetings_user_1'] = $user->get('field_custom_greetings_message')[1]->value;
      $variables['greetings_user_2'] = $user->get('field_custom_greetings_message')[2]->value;

      //Custom Goodbye Messages
      // $variables['goodbye_user'] = $user->get('field_custom_goodby_message');
      $variables['goodbye_user_0'] = $user->get('field_custom_goodby_message')[0]->value;
      $variables['goodbye_user_1'] = $user->get('field_custom_goodby_message')[1]->value;
      $variables['goodbye_user_2'] = $user->get('field_custom_goodby_message')[2]->value;

      //Custom Signature
      // $variables['signature_user'] = $user->get('field_custom_electronic_signatur');
      $variables['signature_user_0'] = $user->get('field_custom_electronic_signatur')[0]->value;
      $variables['signature_user_1'] = $user->get('field_custom_electronic_signatur')[1]->value;


      //Hotel del usuario
      $current_user_hotel = $user ->get('field_hotel_name')->first()->getValue()['target_id'];

      $variables['current_user_hotel'] =  $user ->get('field_hotel_name')->first()->getValue()['target_id'];
      //ksm($current_user_hotel);


      /**
       * Descarga de Reviews y filtrado para enviarlas
       */
      // Load entities by their property values.
      $reviews_for_suggestions = \Drupal::entityTypeManager()
        ->getStorage('node')
        ->loadByProperties(['type' => 'review']);

      foreach ($reviews_for_suggestions as $review_for_suggestions) {

        for ($x = 0; $x <= count($reviews_for_suggestions); $x++) {
          //$check_suggestions = $review_for_suggestions->get('field_hotelname')->first()->getValue()['target_id'];
          $hotel_review =  $review_for_suggestions->get('field_hotelname')->first()->getValue()['target_id'];

          $variables['hotel_review'] = $review_for_suggestions->get('field_hotelname')->first()->getValue()['target_id'];
          //ksm($hotel_review);


          if($review_for_suggestions->get('field_new_proposal')[0]){
            $variables['new_proposal_0'] = $review_for_suggestions->get('field_new_proposal')[0]->value;
          }
          if($review_for_suggestions->get('field_new_proposal')[1]){
            $variables['new_proposal_1'] = $review_for_suggestions->get('field_new_proposal')[1]->value;
          }
          if($review_for_suggestions->get('field_new_proposal')[2]){
            $variables['new_proposal_2'] = $review_for_suggestions->get('field_new_proposal')[2]->value;
          }
          if($review_for_suggestions->get('field_new_proposal')[3]){
            $variables['new_proposal_3'] = $review_for_suggestions->get('field_new_proposal')[3]->value;
          }
          if($review_for_suggestions->get('field_new_proposal')[4]){
            $variables['new_proposal_4'] = $review_for_suggestions->get('field_new_proposal')[4]->value;
          }

        }
      }

    }
  }
}

 

VIEWS-VIEWS-FIELDS--MYVIEWNAME--PAGE.HTML.TWIG

{{ current_user_hotel|render|striptags|trim }}
{{ fields.field_hotelname.content|render|striptags|trim }}

{% if current_user_hotel|render|striptags|trim == fields.field_hotelname.content|render|striptags|trim %}

  <article {{ attributes.addClass(classes) }}>
    <div>
      <div class="new-suggestions-global">

        {# Primera Respuesta #}
        <div class="col-md-6 izq">
          <div class="review-env-global">
            <div class="review-top">
              <table class="table">
                <tr>
                  <th scope="col">
                    {% if fields.field_source.content|render|striptags|trim is not empty %}
                  <td><img src="./sites/default/files/{{ fields.field_source.content|render|striptags|trim }}_icon.png"></td>
                  <td><span class="review-source-name">{{ fields.field_source.content }}</span></td>
                  {% endif %}
                  </th>
                  {% if fields.field_published_date.content|render|striptags|trim is not empty %}
                    <th scope="col">{{ fields.field_published_date.content }}</th>
                  {% endif %}
                  <th scope="col">
                    {% if fields.field_rating_scale.content|render|striptags|trim is not empty %}
                  <td>{{ fields.field_rating_scale.content }}/10</td>
                  {% endif %}
                  <td>
                    {% if fields.field_classification.content|render|striptags|trim == "Positive" %}

                      <img src="./sites/default/files/ranking_optimo.png">
                    {% else %}
                      <img src="./sites/default/files/ranking_medio.png">
                    {% endif %}
                  </td>
                  </th>
                </tr>
              </table>
            </div>
            <div class="review-medio">
              {% if fields.field_reviewer.content|render|striptags|trim is not empty %}
                <span class="reviewer">{{ fields.field_reviewer.content }}</span>
              {% endif %}
              {% if fields.field_review_text.content|render|striptags|trim is not empty %}
                <div class="review-resume">
                  {{ fields.field_review_text.content|render|striptags|trim }}
                </div>
              {% endif %}
              <div id="sug-{{ fields.nid.content|render|striptags|trim }}" class="answers-proposal">
                <span>
                  {% if greetings_user_1 is not empty %}
                    {{ random([greetings_user_0, greetings_user_1, greetings_user_2]) }}
                  {% else %}
                    {{ greetings_user_0 }}
                  {% endif %}
                </span>
                {% if new_proposal_0 is not empty %}
                  <p>{{ new_proposal_0 }}</p>
                {% endif %}
                <p>
                  {% if goodbye_user_1 is not empty %}
                    {{ random([goodbye_user_0, goodbye_user_1, goodbye_user_2]) }}
                  {% else %}
                    {{ goodbye_user_0 }}
                  {% endif %}
                </p>
                <p>
                  {% if signature_user_1 is not empty %}
                    {{ random([signature_user_0, signature_user_1, signature_user_2]) }}
                  {% else %}
                    {{ signature_user_0 }}
                  {% endif %}
                </p>
                <div>
                </div>
              </div>
              <form name="sug-{{ fields.nid.content|render|striptags|trim }}" method="post" onsubmit="return finalsugg()">
                <input id="sug-{{ fields.nid.content|render|striptags|trim }}" type="text" name="finalsugg" value="sug-{{ fields.nid.content|render|striptags|trim }}">
                <input type="submit" value="Submit">
              </form>
              <div class="btn-copiar">

                <a id="btnSugCopy" href="{{ url('<current>') }}" class="btn btn-primary btn-copy"
                   onclick="CopyToClipboard('sug-{{ fields.nid.content|render|striptags|trim }}');">Copiar</a>
              </div>
            </div>
          </div>
        </div>


        {% if new_proposal_1 is not empty %}
          {# Sugeridas Ocultas #}

          <div class="col-md-6 izq ocultas">

            <a id="ver-todas" class="btn btn-default ver-todas-suggestions">Ver todas</a>
            <a id="ocultar-todas" class="btn btn-danger ocultar-todas-suggestions">Ocultar todas</a>


            <div id="suggestions-hide" class="suggestions-hide">
              <div class="resp-1 more-suggestions">
                <div id="sug2-{{ fields.nid.content|render|striptags|trim }}" class="answers-proposal more-proposal">
                <span>
                  {% if greetings_user_1 is not empty %}
                    {{ random([greetings_user_0, greetings_user_1, greetings_user_2]) }}
                  {% else %}
                    {{ greetings_user_0 }}
                  {% endif %}
                </span>
                  <p>{{ new_proposal_1 }}</p>
                  <p>
                    {% if goodbye_user_2 is not empty %}
                      {{ random([goodbye_user_0, goodbye_user_1, goodbye_user_2]) }}
                    {% else %}
                      {{ goodbye_user_0 }}
                    {% endif %}
                  </p>
                  {% if signature_user_1 is not empty %}
                    {{ random([signature_user_0, signature_user_1]) }}
                  {% else %}
                    {{ signature_user_0 }}
                  {% endif %}
                </div>
                <div class="btn-copiar">
                  <a id="btnSugCopy" type="submit" class="btn btn-primary btn-copy"
                         onclick="CopyToClipboard('sug2-{{ fields.nid.content|render|striptags|trim }}')">Copiar</a>
                </div>


                {# Tercera Opcion#}
                {% if new_proposal_2 is not empty %}
                  <div class="resp-2 more-suggestions">
                    <div id="sug3-{{ fields.nid.content|render|striptags|trim }}" class="answers-proposal more-proposal">
                   <span>
                    {% if greetings_user_1 is not empty %}
                      {{ random([greetings_user_0, greetings_user_1, greetings_user_2]) }}
                    {% else %}
                      {{ greetings_user_0 }}
                    {% endif %}
                  </span>
                      <p>{{ new_proposal_2 }}</p>
                      <p>
                        {% if goodbye_user_2 is not empty %}
                          {{ random([goodbye_user_0, goodbye_user_1, goodbye_user_2]) }}
                        {% else %}
                          {{ goodbye_user_0 }}
                        {% endif %}
                      </p>
                      {% if signature_user_1 is not empty %}
                        {{ random([signature_user_0, signature_user_1]) }}
                      {% else %}
                        {{ signature_user_0 }}
                      {% endif %}
                    </div>
                    <div class="btn-copiar">
                      <a id="btnSugCopy" class="btn btn-primary btn-copy" onclick="CopyToClipboard('sug3-{{ fields.nid.content|render|striptags|trim }}')">Copiar</a>
                    </div>
                  </div>
                {% endif %}

                {# Cuarta Opcion#}
                {% if new_proposal_3 is not empty %}
                  <div class="resp-2 more-suggestions">
                    <div id="sug4-{{ fields.nid.content|render|striptags|trim }}" class="answers-proposal more-proposal">
                    <span>
                      {% if greetings_user_1 is not empty %}
                        {{ random([greetings_user_0, greetings_user_1, greetings_user_2]) }}
                      {% else %}
                        {{ greetings_user_0 }}
                      {% endif %}
                  </span>
                      <p>{{ new_proposal_3 }}</p>
                      <p>
                        {% if goodbye_user_3 is not empty %}
                          {{ random([goodbye_user_0, goodbye_user_1, goodbye_user_2]) }}
                        {% else %}
                          {{ goodbye_user_0 }}
                        {% endif %}
                      </p>
                      {% if signature_user_1 is not empty %}
                        {{ random([signature_user_0, signature_user_1]) }}
                      {% else %}
                        {{ signature_user_0 }}
                      {% endif %}
                    </div>
                    <div class="btn-copiar">
                      <a id="btnSugCopy" class="btn btn-primary btn-copy" onclick="CopyToClipboard('sug4-{{ fields.nid.content|render|striptags|trim }}');">Copiar</a>
                    </div>
                  </div>
                {% endif %}
              </div>
            </div>

          </div>
        {% endif %}

      </div>
    </div>
  </article>

{% else %}

"Mensaje adicional"

{% endif %}