Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 4 Articles
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools
How to add a new field to the CMS
Yellow Slider
Join Date: Aug 2006
Posts: 249

 

Show Printable Version Email this Page Subscription
Yellow Slider Yellow Slider is offline 01-21-2011, 10:00 PM

- Apply the changes mentioned in this how-to at your own risk.
- You must have a basic understanding of php, html and vbulletin.
- This is a how-to for vb4.1, and may not work on previous versions.

Everywhere you see FIELDNAME, replace it with your field name (must not contain spaces).

1.
Create a new field in the cms_node table.

2.
If you run vB 4.1.9 or lower , open packages\vbcms\item\content.php and find:
PHP Code:
return $pub_view
Add above it:
PHP Code:
$getFIELDNAME vB::$vbulletin->db->query_first("SELECT FIELDNAME FROM " TABLE_PREFIX "cms_node WHERE nodeid = " $this->nodeid);
$pub_view->FIELDNAME $getFIELDNAME['FIELDNAME']; 
If you run 4.1.10+, you can just create a new plugin with the above code, the hook is vbcms_content_publish_editor.

3.
Go to the template vbcms_edit_publisher.
At the bottom of the template, before these lines:
HTML Code:
</div>
<div style="clear:both"></div>
Add the following lines of code:
HTML Code:
	<div class="blockrow" >
		<label class="thirdleft">FIELD TITLE</label>
		<div class="twothirdsright"><input type="text" size="30" value="{vb:raw FIELDNAME}" name="FIELDNAME" tabindex="1" class="textbox fullwidth" /></div>
	</div>
4.
Go to your ACP --> Plugins & Products --> Add New Plugin.
Hook Location: vbcms_article_save_start.
PHP Code:
PHP Code:
vB::$vbulletin->input->clean_array_gpc('r', array(
    
'FIELDNAME' => TYPE_STR
));

vB::$vbulletin->db->query_write("UPDATE " TABLE_PREFIX "cms_node SET FIELDNAME = '" vB::$vbulletin->db->escape_string(vB::$vbulletin->GPC['FIELDNAME']) . "' WHERE nodeid = " $this->content->getNodeId()); 

The following steps have been written by Lynne, thank you

5. To get the field to spit out, you must add it to the vbcms_content_article_page somewhere (this is rough, you'll have to add your own class/styling).

HTML Code:
<div>Custom Field:{vb:raw FIELDNAME}</div>
6. Then, open vbcms/item/content/article.php and add your field to the end of the protected $content_properties array.
PHP Code:
'imageheight''previewvideo''FIELDNAME' 
7. In that same file, add this line with the other similar lines:

PHP Code:
protected $FIELDNAME
8. Again, in the same file, add this with the other similar functions:
PHP Code:
    public function getFIELDNAME()
    {
        
$this->Load(self::INFO_CONTENT);
        return 
$this->FIELDNAME;
    } 
9. Then add another plugin using hook_location "vbcms_article_populate_start":
PHP Code:
$view->FIELDNAME $this->content->getFIELDNAME(); 
I think I got that all correct.


That's it, you're done.
Reply With Quote
  #2  
Old 01-22-2011, 03:49 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thank you! I'll have to try this out.
Reply With Quote
  #3  
Old 01-25-2011, 09:08 AM
HouseAddict HouseAddict is offline
 
Join Date: Jul 2008
Posts: 31
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I tried that for 3 fields (instead of one) and I can't get any of the data submitted to save into the database... some troubleshooting of the above code might be needed... have you tried this yourself?
Reply With Quote
  #4  
Old 01-25-2011, 10:06 AM
Yellow Slider Yellow Slider is offline
 
Join Date: Aug 2006
Posts: 249
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by HouseAddict View Post
I tried that for 3 fields (instead of one) and I can't get any of the data submitted to save into the database... some troubleshooting of the above code might be needed... have you tried this yourself?
Please paste the relevant lines of code from packages\vbcms\item\content.php, vbcms_edit_publisher and the plugin.
Reply With Quote
  #5  
Old 01-25-2011, 04:31 PM
HouseAddict HouseAddict is offline
 
Join Date: Jul 2008
Posts: 31
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Yellow Slider View Post
Please paste the relevant lines of code from packages\vbcms\item\content.php, vbcms_edit_publisher and the plugin.
Sure.

content.php:
Code:
$geturl_text = vB::$vbulletin->db->query_first("SELECT url_text FROM " . TABLE_PREFIX . "cms_node WHERE nodeid = " . $this->nodeid);
$geturl_pdf = vB::$vbulletin->db->query_first("SELECT url_pdf FROM " . TABLE_PREFIX . "cms_node WHERE nodeid = " . $this->nodeid);
$geturl_audio = vB::$vbulletin->db->query_first("SELECT url_audio FROM " . TABLE_PREFIX . "cms_node WHERE nodeid = " . $this->nodeid);
$pub_view->url_text = $geturl_text['url_text'];
$pub_view->url_pdf = $geturl_text['url_pdf'];
$pub_view->url_audio = $geturl_text['url_audio'];


article.php:
Code:
	protected $content_properties = array(
		/*INFO_CONTENT================*/
			'pagetext',	'threadid' , 'blogid', 'posttitle' ,
			'postauthor', 'poststarter', 'postid', 'blogpostid', 'showrating', 'htmlstate',
			'post_posted', 'post_started', 'previewimage', 'imagewidth', 'imageheight', 'previewvideo', 'url_text', 'url_pdf', 'url_audio'
	);

	protected $url_text;

	protected $url_pdf;

	protected $url_audio;

   public function geturl_text()
   {
        $this->Load(self::INFO_CONTENT);
        return $this->url_text;
   }

   public function geturl_pdf()
   {
        $this->Load(self::INFO_CONTENT);
        return $this->url_pdf;
   }

   public function geturl_audio()
   {
        $this->Load(self::INFO_CONTENT);
        return $this->url_audio;
   }


vbcms_content_article_inline template

added this:
Code:
	<div class="blockrow">
		<label class="quarter">Text Transcript URL</label>
		<div class="threequarters"><input type="text" size="30" value="{vb:raw url_text}" name="url_text" tabindex="1" class="textbox fullwidth" /></div>
	</div>

	<div class="blockrow" >
		<label class="quarter">PDF Transcript URL</label>
		<div class="threequarters"><input type="text" size="30" value="{vb:raw url_pdf}" name="url_pdf" tabindex="1" class="textbox fullwidth" /></div>
	</div>

	<div class="blockrow" >
		<label class="quarter">Audio Transcript URL</label>
		<div class="threequarters"><input type="text" size="30" value="{vb:raw url_audio}" name="url_audio" tabindex="1" class="textbox fullwidth" /></div>
	</div>
right below this:
Code:
	{vb:raw editor}
	<vb:else />
	{vb:raw previewtext}
	</vb:if>


plugin with vbcms_article_populate_start hook:
Code:
$view->url_text= $this->content->geturl_text();
$view->url_pdf= $this->content->geturl_pdf();
$view->url_audio= $this->content->geturl_audio();


and plugin with vbcms_article_save_start hook:
Code:
vB::$vbulletin->input->clean_array_gpc('r', array(
    'url_text' => TYPE_STR,
    'url_pdf' => TYPE_STR,
    'url_audio' => TYPE_STR
));

vB::$vbulletin->db->query_write("UPDATE " . TABLE_PREFIX . "cms_node 
SET url_text = '" . vB::$vbulletin->db->escape_string(vB::$vbulletin->GPC['url_text']) . "',
url_pdf = '" . vB::$vbulletin->db->escape_string(vB::$vbulletin->GPC['url_pdf']) . "',
url_audio = '" . vB::$vbulletin->db->escape_string(vB::$vbulletin->GPC['url_audio']) . "' 
WHERE nodeid = " . $this->content->getNodeId());


As I said, the fields show up just fine, but when I enter anything into them and click Save or Apply, the values are not shown.

They are correctly saved in the mysql, but are not being shown in the Edit Article page under those fields when the changes to the article are saved.
Reply With Quote
  #6  
Old 01-25-2011, 04:54 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I have tried this and it works fine for me. Have you tried spitting out your last query to see if it is correct?

This is incorrect:
PHP Code:
$geturl_text vB::$vbulletin->db->query_first("SELECT url_text FROM " TABLE_PREFIX "cms_node WHERE nodeid = " $this->nodeid);
$geturl_pdf vB::$vbulletin->db->query_first("SELECT url_pdf FROM " TABLE_PREFIX "cms_node WHERE nodeid = " $this->nodeid);
$geturl_audio vB::$vbulletin->db->query_first("SELECT url_audio FROM " TABLE_PREFIX "cms_node WHERE nodeid = " $this->nodeid);
$pub_view->url_text $geturl_text['url_text'];
$pub_view->url_pdf $geturl_text['url_pdf'];
$pub_view->url_audio $geturl_text['url_audio']; 
You are using $geturl_text for all three of them. I would actually do this:
PHP Code:
$geturl_text vB::$vbulletin->db->query_first("SELECT url_text, url_pdf, url_audio FROM " TABLE_PREFIX "cms_node WHERE nodeid = " $this->nodeid);

$pub_view->url_text $geturl_text['url_text'];
$pub_view->url_pdf $geturl_text['url_pdf'];
$pub_view->url_audio $geturl_text['url_audio']; 
Reply With Quote
  #7  
Old 01-25-2011, 05:00 PM
HouseAddict HouseAddict is offline
 
Join Date: Jul 2008
Posts: 31
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Doh!

Obvious mistake.

That was it, in a way...

Now, the problem I have is if I put those fields in "vbcms_edit_publisher" template, they show up just fine and work ok. But if I put them in "vbcms_content_article_inline" template - no go, they don't show the values at all, even though they save them properly.

And I'd rather have those extra fields in vbcms_content_article_inline, below the actual content box instead of in vbcms_edit_publisher which adds them into the right-hand column which makes that long column even longer now...

Thoughts?

Maybe this has something to do with the hooks?
Reply With Quote
  #8  
Old 01-25-2011, 05:20 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

If you want them in other templates, you must write a plugin to register them for use in the other template. Cellarius wrote a really good article that you may be interested in - [vB4] Rendering templates and registering variables - a short guide
Reply With Quote
  #9  
Old 01-25-2011, 06:10 PM
HouseAddict HouseAddict is offline
 
Join Date: Jul 2008
Posts: 31
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Lynne View Post
If you want them in other templates, you must write a plugin to register them for use in the other template. Cellarius wrote a really good article that you may be interested in - [vB4] Rendering templates and registering variables - a short guide
And that's a short guide?

Geez... I just spent almost an hour going through it and trying it out without any success.

Tsk, why does this have to be so darn complicated...

Hmm, how can I extend that $pub_view array to be available to other templates, not just vbcms_edit_publisher (as defined in content.php) in an easier manner...

--------------- Added [DATE]1295989658[/DATE] at [TIME]1295989658[/TIME] ---------------

It seems that I am having some issue with spitting out the value in the front-end as well... adding that code to vbcms_content_article_page prints out nothing.

Seems the root of the problem is $view->FIELDNAME = $this->content->getFIELDNAME(); in the plugin attached to vbcms_article_populate_start... as if I change that $view->FIELDNAME value to a constant, I get a value out on the front-end and in all the other templates (weird). If I leave it as $this->content->getFIELDNAME(), it ends up blank.
Reply With Quote
  #10  
Old 01-25-2011, 09:08 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Ah yes, steps 5 through 9 were written to add the fields to the article page. That's it. If you need it on another page, you'd have to probaby do all those steps on another page. Perhaps someone else will look into that part and post it.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:48 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.05289 seconds
  • Memory Usage 2,348KB
  • Queries Executed 25 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (6)bbcode_code
  • (3)bbcode_html
  • (9)bbcode_php
  • (3)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (2)pagenav_pagelink
  • (10)post_thanks_box
  • (2)post_thanks_box_bit
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit
  • (10)post_thanks_postbit_info
  • (9)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete