TransWikia.com

How do you override the add or edit node for a particular content type

Drupal Answers Asked by Dupls on February 8, 2021

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 DrupalCoreFormFormStateInterface;

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.

One Answer

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, DrupalCoreFormFormStateInterface $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';
  }
}

Answered by adrum99 on February 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP