drupal|April 27, 2020|1 min read

Drupal - How to rename column of a content type

TL;DR

Drupal does not support renaming fields through the UI; use a database update query on the config table to rename the field machine name and label.

Drupal - How to rename column of a content type

Introduction

You already have a content type with one or more fields in it. Later, you realize you should rename it due to whatever reason. Lets take an example:

  • A text field(long text area)
  • Image field

Drupal provides no way to rename existing fields of a content type. But, you can do this programatically or by a db query.

Programmatic Solution

Do following steps:

  • Add the fields into your content type, with the name you want
  • See below php code for drupal
$query = db_select( 'node', 'n' );
  $query
    ->condition( 'n.type', 'article' )
    ->fields('n', array('nid'));
  $result = $query
     ->execute()
   ->fetchAll();

  foreach( $result as $row ) {
    $nd = Drupal\node\Entity\Node::load($row->nid);
  
    $nd->body = $nd->field_article_introduction;
    $nd->field_image = $nd->field_top_image;
    $nd->save();
  }

The idea is to fetch all such nodes for that particular content type, and assign old values to the new fields you just created.

  • You can put multiple fields if you have in the php code above.
  • Make sure you have copied content, by loading one or two nodes
  • Find out all the views you have created wiht old fields. You need to put new fields with same settings.
  • Find out the occurrences of those old fields in your custom theme code.
  • Now, delete old fields from your content type

Clear the cache and test it.

Related Posts

Drupal 8 - How to Theme Form and its Fields with reordering fields

Drupal 8 - How to Theme Form and its Fields with reordering fields

Introduction In this post, we will see how to theme form and its fields…

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Introduction I needed a report page, where I wanted to have some information…

Drupal: How to detect country and redirect to country specific website by using Cloudflare

Drupal: How to detect country and redirect to country specific website by using Cloudflare

Introduction Assume you have a drupal website and using cloudflare. You are…

Drupal 8 - How to hide help link About text formats and text format guidelines

Drupal 8 - How to hide help link About text formats and text format guidelines

Problem In drupal textarea field, it was always a pain to see the two links…

Drupal 8 Smart Image Style - Handle aspect ratio for small and long images

Drupal 8 Smart Image Style - Handle aspect ratio for small and long images

Problem Statement I’ve been using image styles, and heavily used “Scale and crop…

Drupal 8 Comment module - How to configure comments module from ugly to beautiful - Theming comments module

Drupal 8 Comment module - How to configure comments module from ugly to beautiful - Theming comments module

Introduction Drupal provides a powerful comment module, which comes as a part of…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…