1

Wasn't sure exactly what to search for in trying to find recommendations for this.

I have created a custom content entity type with some numerical fields. The canonical view for this entity uses "views_data" = "Drupal\views\EntityViewsData" as specified in the entity definition annotation. This entity type does not depend on node. Some modifications are made to the display of the field data using hook_entity_view_alter and hook_page_attachments. This is all working fine.

What I would like to do is two things:

1) add an additional HTML section to the canonical view, below the display of all the entity parameters, which displays the results of a database query. The query being executed will depend on the content of the entity being loaded. Currently I am just using "echo" to test, but that spits the results out at the top of the document.

2) add a form to the canonical view containing one text field, one dropdown, and one button. The contents of the dropdown will also depend on the entity field content. The button should perform some calculations on the numerical fields based on the form inputs, and adjust the build array contents accordingly after reloading the page.

I am not really sure what approach to take here or how to execute it, and would like to ask for recommendations and examples to look at. Should I be creating a custom class that extends Drupal\views\EntityViewsData to accomplish all of this? Should I be doing this with Twig instead (which I am not very familiar with and can't seem to find many examples similar to what I'm trying to do)? Would using jQuery instead be a better approach?

1 Answer 1

2

This would be the answer to the two questions:

1) add additional HTML to the canonical view

When using hook_ENTITY_TYPE_view() you have a render array and you can place new array keys with additional render elements. Don't use echo for testing, put it in the build array like this:

$build['text1'] = ['#markup' => '<p>Text 1</p>'];

2) add a form to the canonical view

Build a form class for this form. Then you can place the form also in hook_ENTITY_TYPE_view():

$build['myform'] = \Drupal::formBuilder()->getForm('Drupal\mymodule\Form\MyForm');

You can pass additional parameters in getForm(), if you need information from the entity to build the form.

I don't understand what you are trying to do with Drupal\views\EntityViewsData. Other than mentioning this one class you are not writing anything else about views.

1
  • 1
    Thanks, this is exactly the info I needed to get into developing what I want it to do. The point about the views was just a note in case that info was relevant for determining an approach.
    – saramm1
    Commented Aug 30, 2016 at 18:02

Your Answer

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

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