Difference between revisions of "Adding fields"

From AV Scripts Wiki
Jump to: navigation, search
(Check if it works)
(Display the value in a template)
 
Line 72: Line 72:
 
On the videos page the new field could be displayed with this code:
 
On the videos page the new field could be displayed with this code:
  
     {{ video.myField }}
+
     <nowiki>{{ video.myField }}</nowiki>
  
 
When editing the template you have chosen, see what the content variable is called by reading the template. It will likely just be called something similar to the content type like "game" or "wallpaper". Notice also how we don't use the underscore when outputting the value in a template.
 
When editing the template you have chosen, see what the content variable is called by reading the template. It will likely just be called something similar to the content type like "game" or "wallpaper". Notice also how we don't use the underscore when outputting the value in a template.

Latest revision as of 14:18, 17 February 2016

If you want to add a field to a form in the admin panel and have that value save to the database you can do so without massive code changes.

For the sake of this guide, we will be adding a new field called "my_field" to the database and a form field to set that value. You should replace instances of "my_field" and "myField" with your field name.

Adding the field to the database

Firstly you will want to add a new column to your database called "my_field". This will need to go on the right table. For the guide we will add it to the videos table called avcms_videos. The table names for other content types are formatted exactly as you'd expect: the prefix you chose when installing the script (defaults to "avcms_") followed by the name of the content such as "videos" or "games".

You can add your new column to the database using PHPMyAdmin or similar software. The exact details of MySQL column creation are outside of the scope of this tutorial, it is assumed you know how to do this.

Adding the getter and setter

To allow AVCMS to write to the newly created column two methods need to be added to the content entity. Every content type in AVCMS is represented by an object. For example, the Video object can be found at:

src/AVCMS/Bundles/Videos/Model/Video.php

You will find the content model you are looking for in a similar location, for example:

Games: src/AVCMS/Bundles/Games/Model/Game.php Wallpapers: src/AVCMS/Bundles/Wallpapers/Model/Wallpaper.php

When tracking those files down you will see there is one file with the singular name (Video) and one file with the plural name (Videos). You want the file with the singular name.

In these files you will see many methods that look similar to this:

   public function setMyField($value)
   {
       $this->set("my_field", $value);
   }
   
   public function getMyField()
   {
       return $this->get("my_field");
   }

The above code shows you the two PHP methods that need to be added to support the "my_field" column. You can modify the code to replace the instances of "my_field" and "MyField" with your field name.

Note how the method names are the same as the column name in the database but with the underscore removed. We also capitalise the separate words but this is optional as PHP methods are case-insensitive.

Adding the field to the form

This tutorial assumes you are editing the form for a content type in the admin panel. Every form has it's own file. To edit the add content and edit content form, you will need to track down the file similar to this:

src/AVCMS/Bundles/Videos/Form/VideoAdminForm.php

Replace "Video" above with the content type you are modifying to find the correct file.

In this file you will be able to add your new field. You can find documentation on what field types can be added on this website: http://andyvenus.github.io/form/fields/

For this tutorial, we will add a simple text field:

   $form->add('my_field', 'text', [
       'label' => 'My Field'
   ]);

Notice how the first parameter matches the database column name. You will need to update that with your column name.

You can also set the label that will appear on the form by modifying the 'My Field' part of the code above.

Check if it works

These are all the steps needed to add a new field. The field should now save to the database when you submit the form. Check this by saving and refreshing the page.

Display the value in a template

To display your new value, you will need to edit a template. This tutorial will assume you want to modify the main template where the content is displayed. For videos, the watch video template is located at

src/AVCMS/Bundles/Videos/resources/templates/watch_video.twig

Templates for other content types are named somewhat similarly, although they will be called things like "play_game" or "wallpaper_details".

On the videos page the new field could be displayed with this code:

   {{ video.myField }}

When editing the template you have chosen, see what the content variable is called by reading the template. It will likely just be called something similar to the content type like "game" or "wallpaper". Notice also how we don't use the underscore when outputting the value in a template.