0

So due to very rare use-case client issues, we've had to use a somewhat hacky solution to solve one of our goals. I am looking for a solution (hacky is fine) to solve one of the problems created by this solution. This is in Drupal 8 for reference.

I'll give the breakdown of where we are, and what sort of solution we're trying to achieve:

We needed to essentially duplicate the functionality of the menu system based on dynamic values on a given page, based on relevant field values. Occasionally, this would have to be done several times over.

To do this, we used a module that allowed us to place HTML elements inside the node display to wrapper several fields that would all be displayed inside this "menu", so that they'd have the proper menu markup.

We also created a custom display suite template to display each row properly (i.e. it contains the menu markup for each row) and we're using that as the field widget for each given field.

So it looks like this in the Manage Display page:

div
    ul
         FIELD (with the custom DS template widget)

Here's the problem:

We also need to have a couple of static links, based on the URL with some arbitrary additional pathing afterwards, which we're currently doing in the custom template (unfortunately with logic in the template).

Looks like this:

{% if element['#field_name'] == 'field_name_1' or element['#field_name'] == 'field_name_2' %}
    <li class="menu-item {{ element['#field_name'] }}" data-responsive-menu="accordion large-dropdown"><a href="{{ url('<current>') }}/url1" >Link1</a></li>
    <li class="menu-item {{ element['#field_name'] }}" data-responsive-menu="accordion large-dropdown"><a href="{{ url('<current>') }}/url2" >Link2</a></li>
    {%endif %}

The only problem is that since this requires a given field to have a value in it, and the links always need to be rendered, we want to know if there's a way to insert a value so that a specific field is never truly empty.

We were thinking of putting in &NoBreak; for markup. How would we go about checking if a field is empty, and if so, inserting custom markup into it so that the template always renders the field?

We tried HOOK_node_view_alter(), but that gives us a render array that isn't particularly easy to traverse or modify (though maybe this is the solution, I'm not sure). We've tried putting in a

$build['#post_render'][] = 'MY_MODULE_node_post_render';

hook, but that seems to give us the HTML in a way that isn't easily changeable.

So the question is - how do we put custom markup in an empty field to be displayed in a given entity view mode, but display the normal results of the field if there are results?

1 Answer 1

0

This is what I eventually did to get it to work.

function MY_MODULE_entity_display_build_alter(&$build, $context) {
  $view_mode = $context['view_mode'];
  $object = $context['entity'];
  if (empty($build['field_machine_name']['#field_type'])) {
    $field = 'field_machine_name';
    $build['field_machine_name'] = global_events_faux_element($build, $field, $view_mode, $object);
  }
}

function MY_MODULE_faux_element($build, $field, $view_mode, $object) {
  $element = array(
    '#theme' => "field",
    '#title' => "Field Name Here",
    '#label_display' => "hidden",
    '#view_mode' => $view_mode,
    '#language' => "en",
    '#field_name' => $field,
    '#field_type' => "text_long",
    '#field_translatable' => FALSE,
    '#entity_type' => "node",
    '#bundle' => "bundle_name",
    '#formatter' => "smart_trim",
    '#is_multiple' => FALSE,
    '#object' => $object,
    '0' => array(
      '#markup' => "&NoBreak;",
    ),
    '#weight' => 2,
  );
  return $element;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.