PDA

View Full Version : How to add a new field to the CMS


Yellow Slider
22 Jan 2011, 10:43
- 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:

return $pub_view;

Add above it:

$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:

</div>
<div style="clear:both"></div>

Add the following lines of 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:

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).

<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.
'imageheight', 'previewvideo', 'FIELDNAME'

7. In that same file, add this line with the other similar lines:

protected $FIELDNAME;



8. Again, in the same file, add this with the other similar functions:
public function getFIELDNAME()
{
$this->Load(self::INFO_CONTENT);
return $this->FIELDNAME;
}

9. Then add another plugin using hook_location "vbcms_article_populate_start":
$view->FIELDNAME = $this->content->getFIELDNAME();I think I got that all correct.


That's it, you're done.

Lynne
22 Jan 2011, 16:49
Thank you! I'll have to try this out. :)

HouseAddict
25 Jan 2011, 10:08
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?

Yellow Slider
25 Jan 2011, 11:06
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.

HouseAddict
25 Jan 2011, 17:31
Please paste the relevant lines of code from packages\vbcms\item\content.php, vbcms_edit_publisher and the plugin.

Sure.

content.php:

$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:

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:

<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:

{vb:raw editor}
<vb:else />
{vb:raw previewtext}
</vb:if>




plugin with vbcms_article_populate_start hook:

$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:

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.

Lynne
25 Jan 2011, 17:54
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:
$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:
$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'];

HouseAddict
25 Jan 2011, 18:00
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?

Lynne
25 Jan 2011, 18:20
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 (http://www.vbulletin.org/forum/showthread.php?t=228078)

HouseAddict
25 Jan 2011, 19:10
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 (http://www.vbulletin.org/forum/showthread.php?t=228078)

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 1295989658 at 1295989658 ---------------

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.

Lynne
25 Jan 2011, 22:08
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.

Miss T
04 Feb 2011, 09:32
hey Yellow :)

cant you please check you PM inbox - you qouta is up and the reply you asked me for cannot be sent to you :)

edyy
11 May 2011, 19:09
I can't manage to make this to work, I've made everthing it says in here but when I put the data into the fields and push SAVE I get this error
Invalid SQL:
UPDATE for_cms_node
SET url_prod = 'test1',
url_image = 'test2',
WHERE nodeid = 21;

MySQL Error : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE nodeid = 21' at line 4
Error Number : 1064

Lynne
11 May 2011, 20:22
That's because you have a comma right before the WHERE line which isn't supposed to be there.

edyy
12 May 2011, 14:30
The problem it is as you said a comma, thank you verry much for the help. Now i have the same problem as HouseAddict, the date is inserted into the database but is not spit out on the article page. Can you help me fix that problem too ?

Thanks again for the help.

Lynne
12 May 2011, 22:57
I haven't any need for adding a new field to my CMS, so I haven't looked into this in anyway past what I posted for Yellow Slider.

edyy
05 Jun 2011, 14:55
I've tried the exact example and the date still isn't spit out in the article page.

ohadpartuck
11 Apr 2012, 12:32
Hi lynne,
I have the same problam,
I managed to save it but not to spit it out.
I didn't quiet understood number 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>

I have added <p>{vb:raw articlesource}</p>
and nothing..

any help?

charlesr
03 May 2012, 09:47
I'm desperate for custom article fields and ways to manage the CMS better - if this existed, it would make the CMS amazing. Currently I'd just describe the CMS as functional, albeit simple and easy to use.

Anyone else reading this thread, please vote and watch and comment here: http://tracker.vbulletin.com/browse/VBIV-10048

We have spent a lot of time making the VB4CMS look fantastic, and now just need it to BE fantastic! I can't spend time manually making new fields in the database - the long term implications are just too tricky.

firebrand media
03 May 2012, 15:26
1.
Create a new field in cms_node
Off to a bad start since I don't even understand step 1.
Could someone explain?
Is this something I do in the control panel?
Do I need to create a plugin?
Modify a template?

kh99
03 May 2012, 15:55
Off to a bad start since I don't even understand step 1.
Could someone explain?
Is this something I do in the control panel?
Do I need to create a plugin?
Modify a template?


These instructions involve manually modifying the database and editing vb files as well as template modifications and creating plugins. It took me a bit to understand that the first step is to add a column to the cms_node table in the database (could probably use some clarification there).

firebrand media
03 May 2012, 23:17
Ah...
So I just add a column to the cms_node table?
Easy enough.

This is my first foray into vBulletin modding and I just get nervous about operating with a chainsaw. :)

--------------- Added 1336088225 at 1336088225 ---------------

Sorry for the noob questions.
2.
Open packages\vbcms\item\content.php and find:
If I edit conten.php, isn't the change going to get blown away when update vBulletin?

kh99
04 May 2012, 01:23
If I edit conten.php, isn't the change going to get blown away when update vBulletin?

Yes. That's why it's good to use plugins when possible, but there isn't always a hook location in place to do what you want to do.

Lynne
04 May 2012, 02:29
And this is why you should always keep notes when editing your site.

ragtek
04 May 2012, 19:56
anybody got this running without editing the files?

charlesr
05 May 2012, 08:24
I don't think it's possible because there is no hook location to call via a plugin product.

ragtek
05 May 2012, 11:39
1. edit isn't necessary => hook vbcms_content_publish_editor
2. template edit is fine, who cares about this ( tms^^ )

but everythng else seems to be impossible
shame that they still didn't change this:(

Yellow Slider
27 May 2012, 18:20
1. edit isn't necessary => hook vbcms_content_publish_editor
2. template edit is fine, who cares about this ( tms^^ )

but everythng else seems to be impossible
shame that they still didn't change this:(

Yep, it took a year but they finally added the hook. I've updated the relevant step.

I guess I should have asked them to add some other hooks back then, cause if I open a new hook request now, vB 5 would be out by the time the hooks get added :)

christleo
22 Dec 2012, 14:08
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 1295989658 at 1295989658 ---------------

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.


Having the same issue... $this->content->getFIELDNAME() seems like giving me blank too...

pczone
25 Apr 2014, 09:38
I also blank too , I try to fixed and works.

edit:
packages/vbcms/item/content/article.php

find:
$sql = "SELECT node.nodeid, node.showrating, node.setpublish, node.new

add node.FIELDNAME, at the end of the line:
$sql = "SELECT node.nodeid, node.showrating, node.setpublish, node.new,node.FIELDNAME,

BlackxRam
07 Jun 2015, 03:12
Can anyone confirm that this mod still works? How many custom fields does this add to articles in your CMS?

SilverBoy
03 Sep 2015, 02:43
I used this steps before (vb4.2.0) and it works like a charm, but today when I want to reused in newer version (4.2.1) it doesn't work.
I have problem in update to database process, it didn't save anything in database, any hints?

Budget101
19 Mar 2017, 23:44
So, once you've successfully gotten the fields to populate in the database, what variable or phrase do you use to call them into play?? I have myfield1 but when I try to use {vb:raw myfield1} it remains blank.