Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the parent element property before I save and published the children element in Silverstripe Elemental?

I am having a little problem using silverstripe elemental. I am trying to get some parent element property in a children element, but only after I save and published the children element, the parent element property becomes available.

For example, I want to get the QuizQuestion element property QuestionNumber = 2 in children element QuizAnswer, but I only get a default value QuestionNumber = 0 in the database.

When I Debug::show($this->QuizQuestion()->QuestionNumber), I can see it is indeed 2. How can I get the parent element property before I save and published the children element?

like image 533
DAMIEN JIANG Avatar asked Jan 27 '26 13:01

DAMIEN JIANG


1 Answers

I don't know now this module but normally you should be able to get the parent id out of the current url. It's very hacky but should work for you.

public function ItemByUrl() {
  $uri = explode('/', $_SERVER['REQUEST_URI']);
  $i = array_search('IDENTIFIER', $uri);

  if(isset($uri[$i+PARTS])) {
    $itemID = $uri[$i+PARTS];
    $item = DATAOBJECTCLASS::get()->byID($itemID);
    if($item) {
      return $item;
    }
  }
}

So if your URL is something like that

admin/.../EditForm/field/ParentDataObject/item/67/ItemEditForm/field/ChildDataObject/item/new

And you want to get the ParentID 67

IDENTIFIER would be "ParentDataObject"

PARTS would be "2"

and DATAOBJECTCLASS would be "ChildDataObject"

like image 100
csy_dot_io Avatar answered Jan 30 '26 03:01

csy_dot_io



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!