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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Widgets /

Discuss SilverStripe Widgets.

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

Using HasManyComplexTableField in a Widget


Go to End


3 Posts   2301 Views

Avatar
Zooney

Community Member, 9 Posts

13 April 2011 at 4:22am

Edited: 13/04/2011 4:29am

Is it possible to use a HasManyComplexTableField in a Widget?

I'm trying to piece together a quick widget which will allow people to create and manage a list of links without dealing with an editor (yes, there's a need for this in my user base...).

The Widget class I have so far is:

class ListWidget extends Widget {
  
  public static $db = array(
    'Title' => 'Varchar'
  );
  
  public static $has_many = array(
    'ListItems' => 'ListItem'
  );
  
  static $title = '';
  static $cmsTitle = 'List of Links';
  static $description = 'Create, Manage, and Title a list of links';
  
  function Title() {
    return $this->Title;
  }
  
  function Items() {
    $output = new DataObjectSet();
    
    $items = $this->ListItems;
    foreach( $items as $item ) {
      $output->push(
        new ArrayData(
          array(
            'Text' => $item->Text,
            'Link' => $item->Link
          )
        )
      );
    }
    
    return $output;
    
  }
  
  function getCMSFields() {
    $fields = new FieldSet();
    
    $titleField = new TextField( 'Title', 'Title' );
    $fields->push( $titleField );
    
    $listItemsField = new HasManyComplexTableField( $this, 'ListItems', 'ListItem', array( 'Text' => 'Text', 'Link' => 'Link' ) );
    $fields->push( $listItemsField ); 
    
    return $fields;
  }
  
}

...and the ListItem DataObject looks like this:

class ListItem extends DataObject {
  
  public static $db = array(
    'Text' => 'Varchar',
    'Link' => 'Varchar'
  );
  
  public static $has_one = array(
    'ListWidget' => 'ListWidget'
  );
  
}

This all seems like it should work, but when I try to add the Widget to a WidgetArea I get the following error:

Fatal error: Call to a member function FormAction() on a non-object in C:<webroot>\sapphire\forms\FormField.php on line 98

Any idea as to what's going on here?

Avatar
Zooney

Community Member, 9 Posts

13 April 2011 at 8:35am

Hrrm,...

I think I've come to the conclusion that what I'm trying to do is simply impossible in SilverStripe. I'm not certain if it's because the Widget class isn't under SiteTree or what...

SilverStripe widgets have a fairly odd implementation... they're just not what I expected them to be, I guess.

Avatar
vancouverWill

Community Member, 121 Posts

28 July 2011 at 9:42am

any luck on this? I tried some other form fields all referncing dataobjects which would work fine on a page but cause the same error when on a widget. anyone else had any success with this?

Call to a member function FormAction() on a non-object

cheers Will