The Link

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
 /**
 * CategoryLink
 */

class CategoryLink extends DataObject {
 
  static $plural_name = 'Category Links';
  static $singular_name = 'Category Link';

  //Relations
  static $db = array (
    'Name' => 'Varchar(100)',
    'URL' => 'Varchar(100)'
  );

  static $has_one = array (
    'LinksCategory' => 'LinksCategory',//link can only have one category
    'LinkImage' => 'Image'
  );
   
  public function getCMSFields_forPopup() {
    return new FieldSet(
      new TextField('Name','Link Name'),
      new TextField('URL','Web Address'),
      new FileIFrameField('LinkImage')
    );
  }

}

The Category

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
/**
 * LinksCategory
 */
 
class LinksCategory extends Page {

  static $has_many = array(
    'MyCategoryLinks' => 'CategoryLink' //one category can have many links
  );

  function getCMSFields() {
    $fields = parent::getCMSFields();

    $manager = new DataObjectManager(
      $this,
      'MyCategoryLinks',
      'CategoryLink',
      array(
        'Name' => 'Link Name',
        'URL' => 'Web Address'
      ),
      'getCMSFields_forPopup'
    );

    $fields->addFieldToTab('Root.Content.Links', $manager);
    return $fields;

  }
 
}

class LinksCategory_Controller extends Page_Controller {
}

Category Holder that outputs all the links

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
/**
 * CategoryHolder
 */

class CategoryHolder extends Page {

  static $allowed_children = array( 'LinksCategory' );

  function GetLinkCategories() {
    $LinkCategories = DataObject::get('LinksCategory');
    return !empty($LinkCategories) ? $LinkCategories : false;
  }

  function GetLinks() {
    $Links = DataObject::get_one("LinksCategory");
    return ($Links) ? DataObject::get("CategoryLink") : false;
  }

}
 
class CategoryHolder_Controller extends Page_Controller {
}