1

I want to format the editing or adding form for the article content type. i.e. node/add/article path and editing the same article.
I copied over the node-edit-form.html.twig template from core/modules/node/template and renamed it node-edit-form--article.html.twig.
I also tried node--article-edit-form.html.twig and some other flavors.
I can override the node.html.twig file by simply copying it to my themes template folder.
But since I couldn't get the edit file to work I also tried overriding the form alter function by adding this in my theme file.

use Drupal\Core\Form\FormStateInterface;

function newsass_form_node_form_alter(&$form, FormStateInterface 
$form_state) {
  $form['#theme'] = ['node_edit_form'];
}

This code is from here- How to override node add template for custom content type?

I added some text on top of the code, but I still can't get the article 'edit' or article 'add' page to display the text.

Drupal 8.6.1, and have debugging enabled. Full explanation of requirement: I have a user entity reference field in the article content type with a list of users that the article can be assigned to. For the most part they have an 'article' role. But if the person leaves, their role will be changed to 'Away'. The article can/will still be assigned to them but I need to indicate that they are no longer with the company or 'Away'. Within views entity reference I am able to re-write the user with the users role but this information doesn't get back to the entity reference field in the editing node. I thought of editing the article node and adding a hook to call the assigned users role..... Which I still need to figure out how to do. With debugging I tried suggested template page--node--add--article.html.twig but I still couldn't get the content revealed and yes I cleared cache. Using page level though doesn't allow granularity for each field.

QUESTION: By mytype is that the machine name of the entity reference field in the content type? I am also not quite understanding this function, where is the call to query the roles? I enabled kint and added this to the function.

kint($elements); die(); 

but get nothing returned. How do I check if this function is actually being called.
I tried something else also. I have users in the entity reference and in the entity reference views for users I added the roles, made them hidden and rewrote the users to include the roles. I works nicely in the views preview, but the roles detail doesn't get carried back to the content type add/edit pages.

  • Which version of drupal are you using? What formatting are you trying to do? If you're using Drupal 8 have you tried enabling twig debugging to see which suggestions are available and which template file is being used? – sonfd Oct 1 '18 at 22:23
0

Depending on where exactly you want to indicate that "away" status, I think you should rather alter the entity reference field widget for that specific field to add this "away" status rather than trying to achieve this via templates.

Sounds like you want to try implementing this hook:

function hook_field_widget_multivalue_WIDGET_TYPE_form_alter(array &$elements, \Drupal\Core\Form\FormStateInterface $form_state, array $context) {
  // Add a css class to widget form elements for all fields of type mytype.
  $field_definition = $context['items']->getFieldDefinition();
  if ($field_definition->getType() == 'mytype') {
    // Be sure not to overwrite existing attributes.
    $elements['#attributes']['class'][] = 'myclass';
  }
}
  • The role is not being passed so this cannot be used. How do I get the additional information from the entity reference view passed to the node? – Dupls Oct 8 '18 at 21:28

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.