Jump to:

796 Posts in 409 Topics by 304 members

Widgets

SilverStripe Forums » Widgets » Widget tab does not appear in admin screen after adding WidgetArea to Page class

Discuss SilverStripe Widgets.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 2148 Views
  • dfondente
    Avatar
    Community Member
    15 Posts

    Widget tab does not appear in admin screen after adding WidgetArea to Page class Link to this post

    I've followed the documentation regarding adding widgets to non-blog pages (http://doc.silverstripe.org/doku.php?id=widgets). I want to add widgets to the base Page class, so I opened mysite/code/Page.php and added:

       public static $has_one = array(
          "Linkarea" => "Pagewidgets"
       );

    Also added:

       function getCMSFields() {
          $fields = parent::getCMSFields();
          $fields->addFieldToTab("Root.Content.Widgets", new WidgetAreaEditor("Pagewidgets"));
          return $fields;
    }

    I run /dev/build?flush=1 and I see this in the output:

    # Table Page: created
    # Field Page.ID: created as int(11) not null auto_increment
    # Field Page.PagewidgetsID: created as int(11) not null default '0'
    # Index Page.PagewidgetsID: created as (PagewidgetsID)
    # Table Page_Live: created
    # Field Page_Live.ID: created as int(11) not null auto_increment
    # Field Page_Live.PagewidgetsID: created as int(11) not null default '0'
    # Index Page_Live.PagewidgetsID: created as (PagewidgetsID)
    # Table Page_versions: created
    # Field Page_versions.ID: created as int(11) not null auto_increment
    # Field Page_versions.RecordID: created as int(11) not null default '0'
    # Field Page_versions.Version: created as int(11) not null default '0'
    # Field Page_versions.WasPublished: created as tinyint(1) unsigned not null default '0'
    # Field Page_versions.AuthorID: created as int(11) not null default '0'
    # Field Page_versions.PublisherID: created as int(11) not null default '0'
    # Field Page_versions.PagewidgetsID: created as int(11) not null default '0'
    # Index Page_versions.RecordID_Version: created as (RecordID,Version)
    # Index Page_versions.RecordID: created as (RecordID)
    # Index Page_versions.Version: created as (Version)
    # Index Page_versions.AuthorID: created as (AuthorID)
    # Index Page_versions.PublisherID: created as (PublisherID)
    # Index Page_versions.PagewidgetsID: created as (PagewidgetsID)

    This is what I would expect. However, when I log in to the admin area and click on one of the cms pages, I expect to see a new Widget tab at the same level as the Main and Metadata tabs under Content, but it's not there. Is there supposed to be a new tab there to manage Widgets on each page?

    I'm new SilverStripe, so maybe I'm missing something. Any help is appreciated.

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: Widget tab does not appear in admin screen after adding WidgetArea to Page class Link to this post

    public static $has_one = array(
    "Linkarea" => "Pagewidgets"
    );

    should be

    public static $has_one = array(
    "Linkarea" => "WidgetArea" // a var Linkarea with the class of 'WidgetArea'
    );

    and you should use Linkarea as your fieldname.

  • dfondente
    Avatar
    Community Member
    15 Posts

    Re: Widget tab does not appear in admin screen after adding WidgetArea to Page class Link to this post

    Thanks for the response. I actually had made some edits to my code when I pasted it in my original post, and introduced the error that you pointed out. Here's the content of my actual Page.php file:

    class Page extends SiteTree {
       
       public static $db = array(
       );
       
       public static $has_one = array(
          "Pagewidgets" => "WidgetArea"
       );
       
    }

    class Page_Controller extends ContentController {
       
       public function init() {
          parent::init();

          // Note: you should use SS template require tags inside your templates
          // instead of putting Requirements calls here. However these are
          // included so that our older themes still work
          Requirements::themedCSS("layout");
          Requirements::themedCSS("typography");
          Requirements::themedCSS("form");
       }
       
       /**
        * Site search form
        */
       function SearchForm() {
          $searchText = isset($_REQUEST['Search']) ? $_REQUEST['Search'] : 'Search';
          $fields = new FieldSet(
           new TextField("Search", "", $searchText)
           );
          $actions = new FieldSet(
           new FormAction('results', 'Search')
           );

           return new SearchForm($this, "SearchForm", $fields, $actions);
       }
       
       /**
        * Process and render search results
        */
       function results($data, $form){
           $data = array(
           'Results' => $form->getResults(),
           'Query' => $form->getSearchQuery(),
           'Title' => 'Search Results'
           );

           return $this->customise($data)->renderWith(array('Page_results', 'Page'));
       }

       function getCMSFields() {
          $fields = parent::getCMSFields();
          $fields->addFieldToTab("Root.Content.Widgets", new WidgetAreaEditor("Pagewidgets"));
          return $fields;
    }
       
    }

    So that's what I have, yet I don't see a Widget tab in the admin interface. At one point I commented out the db change like so:

       public static $has_one = array(
    //      "Pagewidgets" => "WidgetArea"
       );

    I did that to force a db update. After flushing the db, I then activated the line again and did another db build to get a clean copy. Still, no widget tab.

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: Widget tab does not appear in admin screen after adding WidgetArea to Page class Link to this post

    So that's what I have, yet I don't see a Widget tab in the admin interface..

    Your getCMSFields needs to be in your model class rather then the controller one

  • dfondente
    Avatar
    Community Member
    15 Posts

    Re: Widget tab does not appear in admin screen after adding WidgetArea to Page class Link to this post

    Well, that sure is an obvious error in hindsight. Thanks for your help. It's all working fine now.

  • pmg
    Avatar
    Community Member
    10 Posts

    Re: Widget tab does not appear in admin screen after adding WidgetArea to Page class Link to this post

    I can see the widgets tab but when i try to save i get "Error saving content" ??
    Can someone point me in the right direction?

  • dfondente
    Avatar
    Community Member
    15 Posts

    Re: Widget tab does not appear in admin screen after adding WidgetArea to Page class Link to this post

    It would help if you include the code here for the your page class that has the WidgetArea, and if you can provide more details about the error. You can have error reports sent to you via email; if you provide that info someone here can probably help you figure out the problem.

  • pmg
    Avatar
    Community Member
    10 Posts

    Re: Widget tab does not appear in admin screen after adding WidgetArea to Page class Link to this post

    Thanks dfondente,
    This is the code in mysite/code/page.php

    <?php

    class Page extends SiteTree {
       
       public static $db = array(
       );
       
        static $has_one = array(
       "Sidebar" => "WidgetArea",
    );
       
    function getCMSFields() {
       $fields = parent::getCMSFields();
       $fields->addFieldToTab("Root.Content.Widgets", new WidgetAreaEditor("Sidebar"));
       return $fields;
    }

       
    }

    class Page_Controller extends ContentController {
       
       public function init() {
          parent::init();

          // Note: you should use SS template require tags inside your templates
          // instead of putting Requirements calls here. However these are
          // included so that our older themes still work
          Requirements::themedCSS("layout");
          Requirements::themedCSS("typography");
          Requirements::themedCSS("form");
       }
       
       /**
        * Site search form
        */
       function SearchForm() {
          $searchText = isset($_REQUEST['Search']) ? $_REQUEST['Search'] : 'Search';
          $fields = new FieldSet(
           new TextField("Search", "", $searchText)
           );
          $actions = new FieldSet(
           new FormAction('results', 'Search')
           );

           return new SearchForm($this, "SearchForm", $fields, $actions);
       }
       
       /**
        * Process and render search results
        */
       function results($data, $form){
           $data = array(
           'Results' => $form->getResults(),
           'Query' => $form->getSearchQuery(),
           'Title' => 'Search Results'
           );

           return $this->customise($data)->renderWith(array('Page_results', 'Page'));
       }
       
    }

    ?>

    2148 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.