Making CCK fields read-only in Drupal 6

Posted on May 14, 2012

Generally, using form alter on any form field and setting #disabled => TRUE works great. However, form alter is too early of a hook to use on CCK's fields. Moreover, if you disable a field, the browser won't even post the value. This can result in annoying validation issues. So why this post? I found that the most popular post on this topic did not work for me. This post applies to Drupal 6. But relax, there's a Drupal 7 technique.

The correct and safe way to do lock changes to a widget is to mark its HTML elements as read only, not as disabled. To do this, we use the readonly attribute. This ensures that the browser posts the value, unlike disabled. But we can't just stop here. We need to make sure the value isn't changed by a crafty user modifying his post request.

Learning Drupal? Subscribe to my Drupal articles, tips and tutorials.

First, let's hook in to the node form so we can add our #after_build callback. Keep in mind that in this example, we're adding our callback to all node forms. You might want to further limit this to a specific content type.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php

/**                                                                           
 * Implements hook_form_alter().                                              
 */                                                                           
function example_form_alter(&$form, &$form_state, $form_id) {                 
  if (isset($form['#node']) && $form_id == $form['#node']->type .'_node_form') {
    $form['#after_build'][] = 'example_after_build';                          
  }                                                                           
}

Let's keep things simple and modify a text widget. For simple widgets, CCK generally uses the key value.

1
2
3
4
5
6
7
8
<?php

function example_after_build($form, &$form_state) {                           
  $field = 'field_example_cck_field';                                         
  $form[$field][0]['value']['#attributes']['readonly'] = 'readonly';          
  $form_state['values'][$field][0]['value'] = $form[$field]['#default_value']['value'];
  return $form;                                                               
}

And that's it! You can now count on your field being readonly. Yet it will still be posted by the browser. And even if a user tries to post a changed value - or uses Firebug to edit the contents of the fields - our build function restores the original value.

Below is the complete code for reference.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<?php

/**                                                                           
 * Implements hook_form_alter().                                              
 */                                                                           
function example_form_alter(&$form, &$form_state, $form_id) {                 
  if (isset($form['#node']) && $form_id == $form['#node']->type .'_node_form') {
    $form['#after_build'][] = 'example_after_build';                          
  }                                                                           
}

function example_after_build($form, &$form_state) {                           
  $field = 'field_example_cck_field';                                         
  $form[$field][0]['value']['#attributes']['readonly'] = 'readonly';          
  $form_state['values'][$field][0]['value'] = $form[$field]['#default_value']['value'];
  return $form;                                                               
}

Comments

AtaAug. 16, 2012, 12:03 p.m.

liked and effective. Its working. Thanks

Reply
OperationsOct. 5, 2012, 6:41 p.m.

Nice & Tricky -- I liked restoring the value !

Reply
IgorJune 12, 2013, 5:33 p.m.

Thanks a lot - google'd it for a while, couldn't find anything. This solved the issue.

Reply
Nick LewisJuly 1, 2013, 11:25 a.m.

This works beautifully for text fields! How about a single Checkbox though? I am guessing that the key "value" doesn't apply to checkboxes?

Reply
silviogutierrezJuly 3, 2013, 1:52 a.m.

Did you try it? it should work. If I recall correctly, individual checkboxes are also stored with a 'value' key.

Reply
HélèneOct. 19, 2013, 7:19 p.m.

I need the field to be open for input at the create node stage but readonly whenever it is open for edit at a later time. Can the form_alter hook be used for when the node is in edit mode?

Reply
silviogutierrezNov. 4, 2013, 4:49 p.m.

It sure can. Just check that $form['#node'] has an nid. If it does, that means you're editing instead of creating. Hope that helps.

Reply
NeilJuly 31, 2014, 7:21 a.m.

I think a easy way.

$form['field_sw_part']['#pre_render'] = array('field_sw_part_readonly');

function field_sw_part_readonly($element) { $element[0]['value']['#attributes'] = array('readonly' => 'readonly', 'class' => 'readonly'); return $element; }

Reply
silviogutierrezJuly 31, 2014, 4:15 p.m.

Neil,

This will not prevent a user from manually editing the POST data and changing the value. It will only make the field readonly in the browser. So data integrity is not guaranteed.

- Silvio

Reply
SpolisceseeOct. 24, 2018, 2:25 a.m.

casino free free casino games for fun free casino slots bonus games win free money no deposit | https://casinogameslist.icu/ - connect to vegas world https://casinogameslist.icu/ - casino games online

Reply
SiccahaxiomgegoOct. 24, 2018, 2:34 a.m.

cw cbd oil where to buy cbd oil cbd book distributors cbd oil florida | https://buy-cbd.ooo/ - cbd oil reviews 2018 https://buy-cbd.ooo/ - where to buy cbd oil near me

Reply
breeryAtrorneOct. 24, 2018, 2:54 a.m.

buy cbd oil uk best cbd oil reviews cbd oil capsules cbd oil for pets | https://cbdonline2018.com/ - full spectrum cbd oil https://cbdonline2018.com/ - plus cbd oil

Reply
SkipalsilmickOct. 24, 2018, 2:54 a.m.

slot machines for home entertainment 50 lions free slots free casino slots bonus games free casino games for fun | https://playonlinecasino24.icu/ - pechanga casino https://playonlinecasino24.icu/ - new no deposit casinos accepting us players

Reply
JEorsewesOct. 24, 2018, 2:47 a.m.

cbd oil legal where to buy cbd oil online koi cbd cbd vape oil | https://buycbdoil24.com/ - cbd oil in texas https://buycbdoil24.com/ - cbd

Reply
InsisteHoniOct. 24, 2018, 2:31 a.m.

play lady luck slot games with bonus spins play slots online for money play free casino slots now | https://webcasinogames.icu/ - online betting sites https://webcasinogames.icu/ - fortune bay casino

Reply
AbinolionsugsOct. 24, 2018, 2:58 a.m.

bonus casino play free slot machines with bonus spins free slot games free slots 777 | https://mycasinogames.icu/ - new online casinos https://mycasinogames.icu/ - free slots no registration

Reply
tribevitbigOct. 24, 2018, 2:52 a.m.

plainridge casino google free casino slot games slots casino games caesars online casino | https://instantcasinoplay.icu/ - free casino games online https://instantcasinoplay.icu/ - 200 free slot games

Reply
SmowlmedeOct. 24, 2018, 2:59 a.m.

cbd hemp oil walmart cbd oil stores near me cbd oil with thc walgreens cbd oil | https://cbdoil.ooo/ - green mountain cbd https://cbdoil.ooo/ - where can i buy cbd oil near me

Reply
GuestdydayOct. 24, 2018, 2:53 a.m.

side effects associated with using prednisolone, prednisolone acetate ophthalmic suspension cheap prednisolone, prednisolone liquid for cats https://buyprednisolone.icu/

Reply
KimBemDec. 2, 2018, 12:49 p.m.
Reply
jordan shoesDec. 2, 2018, 8:59 a.m.

I enjoy you because of all of your efforts on this web page. My mum loves engaging in internet research and it's simple to grasp why. A number of us notice all about the lively method you provide precious ideas on your website and improve participation from other ones about this concept plus our princess is learning so much. Take advantage of the rest of the new year. You are doing a remarkable job. jordan shoes [url=http://www.shoesjordan.us.com]jordan shoes[/url]

Reply
AddesearomoDec. 2, 2018, 9:37 a.m.
Reply
ManBemDec. 2, 2018, 9:58 a.m.

https://installmentloans2018.com/ - installment loans https://carloan2018.com/ - bad credit car loan https://paydayloansnocreditcheck2018.com/ - payday loans no credit check https://onlinepaydayloans2018.com/ - online payday loans

Reply
breeryAtrorneDec. 2, 2018, 10:11 a.m.
Reply
MealsisammarpDec. 2, 2018, 10:56 a.m.
Reply
PrurtustyDec. 2, 2018, 10:59 a.m.
Reply
KimBemDec. 2, 2018, 11:15 a.m.
Reply
JEorsewesDec. 2, 2018, 12:45 p.m.
Reply
French HomeworkDec. 2, 2018, 11:46 a.m.
Reply
AddesearomoDec. 2, 2018, 12:35 p.m.
Reply
breeryAtrorneDec. 2, 2018, 12:35 p.m.
Reply
MealsisammarpDec. 2, 2018, 1:25 p.m.
Reply
PrurtustyDec. 2, 2018, 1:48 p.m.
Reply
KimBemDec. 2, 2018, 2:46 p.m.
Reply
breeryAtrorneDec. 2, 2018, 3:02 p.m.
Reply
AnnaBemDec. 2, 2018, 3:09 p.m.

[url=https://valtrex500.com/]valtrex[/url] [url=https://propecia5.com/]propecia 5 mg[/url] [url=https://tadalafil20mg.com/]cheap tadalafil 20mg[/url] [url=https://accutane40.com/]accutane for sale[/url] [url=https://sildenafilcitrategenericviagra.com/]order sildenafil[/url]

Reply
AddesearomoDec. 2, 2018, 3:34 p.m.
Reply
Write AssignmentDec. 2, 2018, 3:53 p.m.
Reply
MealsisammarpDec. 2, 2018, 3:56 p.m.
Reply
EvaBemDec. 2, 2018, 4:32 p.m.
Reply
PrurtustyDec. 2, 2018, 4:42 p.m.
Reply
ManBemDec. 2, 2018, 4:50 p.m.

https://onlinepaydayloans2018.com/ - payday loan online https://writemyessay2018.com/ - my friend essay writing https://gapinsuranceforcars.com/ - gap insurance for cars https://personalloansforbadcredit2018.com/ - bad credit personal loans

Reply
breeryAtrorneDec. 2, 2018, 5:30 p.m.
Reply
Photo AssignmentDec. 2, 2018, 5:50 p.m.
Reply
MealsisammarpDec. 2, 2018, 6:24 p.m.
Reply
AddesearomoDec. 2, 2018, 6:27 p.m.
Reply
KennethNubDec. 2, 2018, 6:29 p.m.
Reply
Write My PaperDec. 2, 2018, 6:44 p.m.
Reply
JEorsewesDec. 2, 2018, 7:06 p.m.
Reply
JaneBemDec. 3, 2018, 9:35 a.m.
Reply
Adidas NMD Runner Unisex Green BlueDec. 3, 2018, 4:49 a.m.

I definitely wanted to construct a word so as to appreciate you for all the fabulous guidelines you are writing here. My time consuming internet search has at the end of the day been compensated with excellent suggestions to go over with my visitors. I would declare that many of us website visitors actually are truly blessed to dwell in a magnificent place with very many special people with beneficial ideas. I feel somewhat lucky to have used the web page and look forward to tons of more exciting times reading here. Thanks once more for everything. Adidas NMD Runner Unisex Green Blue

Reply
breeryAtrorneDec. 3, 2018, 5:32 a.m.
Reply
MealsisammarpDec. 3, 2018, 6:23 a.m.
Reply
AddesearomoDec. 3, 2018, 6:28 a.m.
Reply
JEorsewesDec. 3, 2018, 6:32 a.m.
Reply
PrurtustyDec. 3, 2018, 6:48 a.m.
Reply
ManBemDec. 3, 2018, 6:52 a.m.

https://loansonline2018.com/ - payday loans online https://paydayloans2018.com/ - cash advances online https://gapinsuranceforcars.com/ - wawanesa auto insurance https://loanswithbadcredit2018.com/ - loans with bad credit

Reply
breeryAtrorneDec. 3, 2018, 8 a.m.
Reply
DouglasFusDec. 3, 2018, 8:15 a.m.
Reply
MealsisammarpDec. 3, 2018, 8:49 a.m.
Reply
refinance auto loanDec. 3, 2018, 8:55 a.m.

auto loan auto loan best auto loans for excellent credit auto loan rate

Reply
JEorsewesDec. 3, 2018, 9:18 a.m.
Reply
JaneBemDec. 3, 2018, 9:40 a.m.
Reply
EvaBemDec. 3, 2018, 9:44 a.m.
Reply
AddesearomoDec. 3, 2018, 9:47 a.m.
Reply
breeryAtrorneDec. 3, 2018, 10:27 a.m.
Reply
MealsisammarpDec. 3, 2018, 11:10 a.m.
Reply
Type Writer PaperDec. 3, 2018, 12:04 p.m.
Reply
JEorsewesDec. 3, 2018, 12:12 p.m.
Reply
PrurtustyDec. 3, 2018, 12:31 p.m.
Reply
AddesearomoDec. 3, 2018, 12:39 p.m.
Reply
breeryAtrorneDec. 3, 2018, 1:07 p.m.
Reply
JEorsewesDec. 3, 2018, 3:09 p.m.
Reply
PrurtustyDec. 3, 2018, 3:21 p.m.
Reply
breeryAtrorneDec. 3, 2018, 3:35 p.m.
Reply
AddesearomoDec. 3, 2018, 3:37 p.m.
Reply
MealsisammarpDec. 3, 2018, 4:03 p.m.
Reply
adidas nmd r1Dec. 4, 2018, 9:57 a.m.

I really wanted to develop a message to thank you for the marvelous items you are giving here. My rather long internet lookup has finally been rewarded with beneficial know-how to exchange with my close friends. I would express that most of us visitors actually are undeniably blessed to dwell in a really good site with so many brilliant people with good strategies. I feel somewhat lucky to have used the website and look forward to so many more pleasurable moments reading here. Thanks a lot again for all the details. adidas nmd r1

Reply
EvaBemDec. 4, 2018, 10:02 a.m.
Reply
Homework For Pre KDec. 4, 2018, 10:08 a.m.
Reply
EvaBemDec. 4, 2018, 10:11 a.m.
Reply
AarongekDec. 4, 2018, 12:26 p.m.
Reply
Buying EssayDec. 4, 2018, 2:15 p.m.
Reply
Paper Back WriterDec. 4, 2018, 2:55 p.m.
Reply
td bank auto loanDec. 4, 2018, 3:28 p.m.
Reply
JaneBemDec. 4, 2018, 3:50 p.m.
Reply
Online HomeworkDec. 4, 2018, 3:50 p.m.
Reply
EvaBemDec. 4, 2018, 4:07 p.m.
Reply
Homeworks Of AmericaDec. 4, 2018, 4:42 p.m.
Reply
CharlesBemDec. 4, 2018, 7:20 p.m.
Reply
auto loan ratesDec. 4, 2018, 9:31 p.m.
Reply
ManBemDec. 4, 2018, 9:42 p.m.

https://paydayloansnocreditcheck2018.com/ - online payday loans no credit check

Reply
White Paper WritersDec. 4, 2018, 10:30 p.m.
Reply
KimBemDec. 5, 2018, 1:41 a.m.
Reply
ManBemDec. 5, 2018, 1:52 a.m.

https://loanswithbadcredit2018.com/ - loans for bad credit https://essaywriter2018.com/ - essay writer

Reply
ManBemDec. 5, 2018, 1:54 a.m.

https://autoloan2019.com/ - auto loans rates

Reply
EvaBemDec. 5, 2018, 1:58 a.m.
Reply
Essay WriteDec. 5, 2018, 2:35 a.m.
Reply
Web Assign UtahDec. 5, 2018, 3:17 a.m.
Reply
Custom EssaysDec. 5, 2018, 3:43 a.m.
Reply
KimBemDec. 6, 2018, 11:56 a.m.
Reply
yeezyDec. 6, 2018, 10:55 a.m.

Thanks for your entire labor on this blog. Betty takes pleasure in working on internet research and it's really easy to understand why. A lot of people know all relating to the compelling medium you make vital information via the blog and as well as increase contribution from some other people about this subject matter and my girl is without a doubt learning so much. Take pleasure in the remaining portion of the year. You have been conducting a superb job. yeezy

Reply
LonnieThymnDec. 6, 2018, 1:16 p.m.

[url=http://cialisnnq.com]buy cialis[/url] buy cialis [url=http://levitrarrr.com]levitra[/url] levitra [url=http://cialisrrr.com]buy cialis[/url] buy cialis [url=http://viagranorx.com]buy viagra[/url] buy viagra

Reply
CharlesBemDec. 6, 2018, 1:44 p.m.
Reply
JaneBemDec. 6, 2018, 1:46 p.m.
Reply
JaneBemDec. 6, 2018, 2:16 p.m.
Reply
LonnieThymnDec. 6, 2018, 2:34 p.m.

[url=http://cialisnnq.com]buy cialis[/url] buy cialis [url=http://levitrarrr.com]levitra[/url] levitra [url=http://cialisrrr.com]buy cialis[/url] buy cialis [url=http://viagranorx.com]buy viagra[/url] buy viagra

Reply
Assignment HelperDec. 6, 2018, 2:59 p.m.
Reply
Salomon ShoesDec. 7, 2018, 10:41 a.m.

Air Jordan 12 Gym Red http://www.jordan12gymred.us.com/ Nike Outlet satore http://www.nikefactoryoutletstoreonline.com/ nike factory outlet store online http://www.nikefactoryoutletstoreonline.us/ Nike Outlet http://www.nikestores.us.com/ jordan 33 http://www.jordan33.us/ Yeezys http://www.cheapjerseysfromchina.us/ nfl jerseys http://www.customnfljerseys.us/ jordan 11 concord 2018 http://www.jordan11concord.us.com/ Jordan 12 Gym Red 2018 http://www.jordan12gymred.us/ Jordan 12 Gym Red 2018 http://www.redjordan12.us/ Yeezys http://www.yeezy.com.co/ Yeezy Shoes http://www.yeezys.us.com/ Yeezy Supply http://www.yeezysupply.us.com/ Yeezy Boost 350 http://www.yeezy-shoes.us.com/ Yeezy Boost http://www.yeezy-boost350.com/ Yeezy Boost 350 V2 http://www.yeezyboost350.us.com/ Yeezy Shoes http://www.yeezybluetint.com/ Adidas Yeezy 500 http://www.yeezy500utilityblack.com/ Adidas Yeezy 500 http://www.yeezy500utilityblack.us/ Nike VaporMax http://www.vapor-max.org.uk/ Salomon UK http://www.salomon-shoes.org.uk/ Salomon UK http://www.salomons.me.uk/ Salomon Shoes http://www.salomonspeedcross4.org.uk/ Off White Jordan 1 http://www.offwhitejordan1.com/ Nike VaporMax http://www.nikevapormax.org.uk/ Nike React Element 87 http://www.nikereactelement87.us.com/ Nike React Element 87 http://www.nikereactelement87.us/ Nike Air Vapormax Plus http://www.nikeplus.us/ Nike Outlet Store http://www.nike--outlet.us/ Nike Outlet http://www.nikeoutletstoreonlineshopping.us/ Nike Outlet http://www.nikeoutletonlineshopping.us/ NBA Jerseys http://www.nikenbajerseys.us/ Air Max 97 http://www.nikeairmax.us/ Nike Air Max 2017 http://www.max2017.us/ Jordan Shoes 2018 http://www.jordan-com.com/ Jordan 11 Concord http://www.jordan11-concord.com/ Cheap Yeezy Boost http://www.cs7boots1.com/ Wholesale Cheap NBA Jerseys http://www.cheapnba-jerseys.us/ Birkenstock Sandals UK http://www.birkenstocksandalsuk.me.uk/ NBA Jerseys http://www.basketball-jersey.us/ Balenciaga http://www.balenciaga.me.uk/ Balenciaga UK http://www.balenciagauk.org.uk/ Balenciaga http://www.balenciagatriples.org.uk/ Balenciaga UK http://www.birkenstocks.me.uk/ Balenciaga http://www.balenciagatrainers.org.uk/ Air Max 270 http://www.airmax270.org.uk/ Yeezy Shoes http://www.adidasyeezyshoes.org.uk/ Adidas Yeezy http://www.adidasyeezyshoes.org.uk/

Salomon Shoes

Reply
LonnieThymnDec. 7, 2018, 10:45 a.m.

[url=http://cialisnnq.com]tadalafil[/url] tadalafil [url=http://cialisnnq.com]cialis generic[/url] cialis generic [url=http://cialisrrr.com]cialis generic[/url] cialis generic [url=http://cialisrpr.com]cialis generic[/url] cialis generic

Reply
LonnieThymnDec. 7, 2018, 10:49 a.m.

http://cialisnnq.com - tadalafil tadalafil http://cialisnnq.com - cialis generic cialis generic http://cialisrrr.com - cialis generic cialis generic http://cialisrpr.com - cialis generic cialis generic

Reply
LonnieThymnDec. 7, 2018, 10:52 a.m.

[url=http://cialisnnq.com]tadalafil[/url] tadalafil [url=http://cialisnnq.com]cialis generic[/url] cialis generic [url=http://cialisrrr.com]cialis generic[/url] cialis generic [url=http://cialisrpr.com]cialis generic[/url] cialis generic

Reply
gazyunadymnDec. 7, 2018, 11:19 a.m.

https://cbdoilamericano.com/# cbd oil near me cbd oil for sale walmart cbd dosage

Reply

Post New Comment