Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

Custom fields in post.

edited February 2014 in Framework

I nead functionality like adding custom field or checkbox on page edit to pages that already exists. And then displaying all pages that contain that custom field. Does someone have some strategy for that ? Maybe post categories or something like that. How to do that?

I was not developing in ElefantCMS some time and forgot everything that i learn.

Thank You!

In the edit page view i wrote additional input:


{"custom checkbox"}:
{"no"} {"yes"}

I found something like handler for that view called handlers/edit, and I wrote :


if ($f->submit ()) {
...
    $wp->wisible=$_POST['wisible'];
  

handler ends with data by Controller - > Versions::add($wp);

Data is stored in database in table webpage :)

Comments

  • The trouble you'll run into with relying on Versions::add($wp) is that the data will only be stored in the versions table, but not in the main webpage table.

    A more reusable approach is to add an extra text not null field to the webpage table, then change the Webpage model (in apps/admin/models) with these two lines:

    class Webpage extends ExtendedModel {
        public $_extended_field = 'extra';
    

    Now the Webpage model can support custom fields. See the ExtendedModel class for more info on that. The User and blog\Post models already use it, for example.

    The next step is to update the add and edit forms. Just before the add form submit button (in the view template), add this line:

    {! admin/util/extended?extends=Webpage&name=Pages !}

    And in the edit form template, add this line:

    {! admin/util/extended?extends=Webpage&name=Pages&values=[extra|none] !}
    

    In the edit handler, you'll also need to add this line right before $wp->put():

    $wp->update_extended ();
    

    That should allow custom fields on the Webpage model, but you'll need to change one more line to apps/admin/views/pages.html so you can create the custom fields. Change this line:

     <p><a href="/admin/add">{"Add Page"}</a></p>
    

    To this:

    <p>
        <a href="/admin/add">{"Add Page"}</a>
        &nbsp;|&nbsp;
        <a href="/admin/extended?extends=Webpage&name=Pages">{"Custom Fields"}</a>
    </p>
    

    That will add a "Custom Fields" link under Tools > All Pages where you can define the custom fields for web pages.

    I didn't test it all, but those are the usual steps to convert a model to have extended fields. Let me know how it goes!

  • Since the blog posts and users already have custom fields, would it make more sense to incorporate this into the pages as well in Elefant itself?

    Initially I thought the need for extra fields on the pages was low compared to the other content types (blog posts might need thumbnails, descriptions, and other metadata, users could need any number of fields). But it seems like being able to add a few extra fields to pages could offer a bit more flexibility in edge cases without impacting performance or complexity by much.

    What do you think?

  • After playing around with it, I think adding custom fields to pages is the way to go. I just added them to Github and put out a 1.3.7 release. I'll post an announcement for the release a bit later, but now you should be able to add custom fields and then access them in layouts via:

    {{ extra->custom_field_name }}
    
  • I get an error from Tools -> Click to Upgrade:

    Error: SQLSTATE[42000]: Syntax error or access violation: 1101 BLOB/TEXT column 'extra' can't have a default value

    Using MariaDB 5.5.35

Sign In or Register to comment.