Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Widgets /

Discuss SilverStripe Widgets.

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

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


Reply


8 Posts   2165 Views

Avatar
dfondente

Community Member, 15 Posts

25 June 2009 at 5:20pm

Edited: 25/06/2009 5:21pm

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.

Avatar
Willr

Forum Moderator, 5513 Posts

25 June 2009 at 6:04pm

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.

Avatar
dfondente

Community Member, 15 Posts

25 June 2009 at 6:42pm

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.

Avatar
Willr

Forum Moderator, 5513 Posts

25 June 2009 at 6:49pm

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

Avatar
dfondente

Community Member, 15 Posts

26 June 2009 at 4:10am

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

Avatar
pmg

Community Member, 10 Posts

14 July 2009 at 6:56am

Edited: 14/07/2009 7:28am

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?

Avatar
dfondente

Community Member, 15 Posts

14 July 2009 at 7:38am

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.

Avatar
pmg

Community Member, 10 Posts

14 July 2009 at 9:00am

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'));
   }
   
}

?>