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.

Data Model Questions /

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

Connecting two DataObjects in one GridField


Go to End


1221 Views

Avatar
eightythree

Community Member, 12 Posts

21 March 2016 at 7:27am

Hello there,
I don’t know how to ask a question precisely but maybe describing my problem help here. So, I’m creating content slider with pagination where content (and that pagination element) is added dynamically. Where this is pretty easy to achieve, I have problem with pagination tho - mainly because slider canvas and pagination container is in different place of my HomePage.

Here’s my code:

HomePage.php:

. . .
  private static $has_many = array (
    'MainSliderItems' => 'MainSliderItem',
  );
. . .

MainSliderItem.php:


. . .
  private static $db = array (
    'MainSliderItem__Title' => 'Text',
    'MainSliderItem__Text' => 'Text',

    'MainSliderItemPagination__Text' => 'Text',
  );

  private static $has_one = array (
    'HomePageMainSliderItems' => 'HomePage'
  );

  private static $has_many = array (
    'MainSliderPaginationItems' => 'MainSliderPaginationItem'
  );

. . .

  public function getCMSfields() {
    $fields = FieldList::create(

      TextField::create('MainSliderItem__Title','Slider Title')
      TextareaField::create('MainSliderItem__Text','Slider Text')
      TextareaField::create('MainSliderItemPagination__Text','Pagination Text')
    );

    return $fields;
  }

. . .

MainSliderPaginationItems.php:


class MainSliderPaginationItem extends DataObject {
  private static $has_one = array (
    'HomePageMainSliderPaginationItems' => 'MainSliderItem'
  );
}

Page.ss: (templates folder)


 <header>
 . . .

<% if $URLSegment == 'home' %>
<% include Slider %>
<% end_if %>

</header>

$Layout
<% include Footer %>
. . .

HomePage.ss: (layout folder)


<% if MainSliderItems %>
<% loop MainSliderPaginationItems %>

<div class="main-pagination">
  <div class="main-pagination-item">      
    <p>$MainSliderItemPagination__Text</p>
  </div>
</div>

<% end_if %>
<% end_loop %>

Slider.ss: (includes folder)


. . .
  <% loop MainSliderItems %>
  <div class="item">
          <h1>$MainSliderItem__Title</h1>
          <p>$MainSliderItem__Text</p>
  </div>
  <% end_loop %>

. . .

So, is it possible to add new content with GridField in “MainSliderItems” (and “MainSliderPaginationItems” at the same time) and somehow SS will know that “MainSliderPaginationItems” is glued to it and add that content to the right place on page. It’s hard to explain this for me especially since english is not my native language. Slider is working perfectly but I don’t know how to connect those two DataObjects that pagination loop works too. I’ll try to answer all questions about code.

Thanks in advance,
Darek.