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

Adding fields to blog_post

edited November 2012 in Miscellaneous

Hi,

I am working on creating a modification/extension using the blog app. I did some copying and search and replace in the respective files. That all works fine. As I want to be able to filter and search the custom fields, I am unable to use the 'ExtendedModel' (json encoded extra field).

So I went ahead and added a field (to start with) to the post table. In the handlers/edit.php I have added the field as well (as also in the views/edit.php file.

I however don't see the field in the database being "filled". I suspect I need to define the field elsewhere as well, but as I am new to the elefantcms framework I am uncertain where. Any help/pointers into the right direction are appreciated.

Once the project is ready I will let you know where to find it. I will be developing some dynamic content types for this project as well.

With regards,

Taco

Comments

  • Not 100% sure how the post model functions without looking at the code right now (on phone), but did you update the model as well? Might want to give that file a quick run through and see of the logic is working with your custom field.

  • No, I did not extend the model, so that is probably what is missing. As the other fields are not defined in the model (Post.php) file, it is not trivial for me how to extend the model. The only "howto" I have found in the elefantcms documentation is extending the model by adding a field to the json encoded extra field.

    So if someone could share with me the code to extend the Post model with a char(32) field called 'channel', that would help me a lot.

  • I think what @shortj means is if you're adding extended fields via $post->ext ('thumbnail', '/files/posts/my-image.jpg') then you also have to call $post->put () in order for them to be stored.

    For example, this ought to work to set and retrieve an extended field:

    <?php
    
    // get post with ID 1
    $post = new blog\Post (1);
    
    // set an extended field
    $post->ext ('thumbnail', '/files/posts/my-photo.jpg');
    
    // save to the database
    $post->put ();
    
    // access the field
    echo $post->ext ('thumbnail');
    
    ?>
    

    Btw, been making good progress on the UI for extended fields in the custom_fields branch. There's still no UI for defining the fields, but fields manually added to the extended_fields database table are working completely and the blog app has already been converted (and the table is only needed for the forms, not for accessing the data later, so it won't affect performance at all :)).

    Once the UI for defining custom fields is ready, I'll convert the other apps and merge to master!

    And extended fields can now be sent to templates just like this:

    <?php
    
    $post = new blog\Post (1);
    
    echo $tpl->render (
        'blog/post',
        $post->orig ()
    );
    
    ?>
    

    And the view template:

    <h1>{{title}}</h1>
    
    <!--
        if thumbnail is an extended field,
        it's accessible just like any other.
    -->
    <img src="{{thumbnail}}" align="left" />
    
    {{body|none}}
    

    This should really make it easy to extend any of the core object types :)

  • Ah, should have read the original post a bit closer (my head is in other code atm...).

    To add a field to the blog posts, you'd need to edit these:

    1. The database schema itself (obviously ;))

    2. The view template of the form, to add the input for it (e.g., apps/blog/views/add.html and apps/blog/views/edit.html).

    3. The form validations (e.g., apps/blog/forms/add.php and apps/blog/forms/edit.php) if you want to add your own.

    4. In the edit form, you'll have to set the value on the blog post object, for example look at apps/blog/handlers/edit.php around lines 36-41. The add form should be fine without modifications.

    That should be everything you need to change. Let us know how it goes!

Sign In or Register to comment.