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!

Related Questions

Make node only accesible by path alias

3  Asked on November 19, 2021 by kevin-ramirez-zavalza


Form and gated content

1  Asked on November 17, 2021 by dataguy


Calculate Star rating based on webform submissions

1  Asked on November 17, 2021 by brainhax


Pager not working when $form[‘table’] is called with AJAX

1  Asked on November 17, 2021 by user3428971


Send message notification when license expired

1  Asked on November 15, 2021 by learner123


View filtering, Relationship Vs Contextual filter

1  Asked on November 15, 2021 by bassem-mohamed


Missing some block fields

1  Asked on November 15, 2021 by metalbearsolid


Custom template for each profile2 profile type edit form

1  Asked on November 13, 2021 by stavovei-ciprian


How to save an image resource

1  Asked on November 13, 2021 by kosher


How to alter node body content through Views pre render hook?

1  Asked on November 11, 2021 by pramodg


Set Default Value for Image Field contain Multiple Images

1  Asked on November 11, 2021 by pglatz


Ask a Question

Get help from others!

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