# Get parent page title from Model

**URL:** <https://forum.getkirby.com/t/get-parent-page-title-from-model/20142>\
**Category:** Questions\
**Tags:** v3\
**Created:** [November 24, 2020, 8:55pm UTC](https://forum.getkirby.com/t/get-parent-page-title-from-model/20142 "2020-11-24T20:55:31Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![thewebprojects](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thewebprojects/32/10107_2.png) [@thewebprojects](https://forum.getkirby.com/u/thewebprojects)\
**Post date:** [November 24, 2020, 8:55pm UTC](https://forum.getkirby.com/t/get-parent-page-title-from-model/20142/1 "2020-11-24T20:55:31Z")

</div>

Hello. I’m working with a model to dynamically create the title. How do I get the value of the parent title? `$this->parent()->title()` doesn’t seem to work, getting a message that reads, “Using $this when not in object context”. Any insight would be appreciated.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 24, 2020, 9:00pm UTC](https://forum.getkirby.com/t/get-parent-page-title-from-model/20142/2 "2020-11-24T21:00:50Z")

</div>

Could you please post your complete code? Thank you!

---

<div class="post-metadata">

**Author:** ![thewebprojects](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thewebprojects/32/10107_2.png) [@thewebprojects](https://forum.getkirby.com/u/thewebprojects)\
**Post date:** [November 24, 2020, 9:02pm UTC](https://forum.getkirby.com/t/get-parent-page-title-from-model/20142/3 "2020-11-24T21:02:10Z")

</div>

Sure. I’m looking to use the page’s parent title and the current page’s start and end dates.

```
<?php
use Kirby\Cms\Page;
class SessionPage extends Page
{
  public static function create(array $props)
	{
		$start_date = strtotime($props['content']['camp_start_date']); 
		$start_date = date('m/d/Y', $start_date);
		$end_date = strtotime($props['content']['camp_end_date']); 
		$end_date = date('m/d/Y', $end_date);

		$props['content']['title'] = $this->parent()->title() .' '. $start_date .' - ' . $end_date;
		$props['slug'] = uniqid();
		$props['draft'] = false;
		return parent::create($props);
	}
}
```

---

<div class="post-metadata">

**Author:** ![thewebprojects](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thewebprojects/32/10107_2.png) [@thewebprojects](https://forum.getkirby.com/u/thewebprojects)\
**Post date:** [November 24, 2020, 9:04pm UTC](https://forum.getkirby.com/t/get-parent-page-title-from-model/20142/4 "2020-11-24T21:04:37Z")

</div>

I should note that I’m using the [Custom Add Fields Plugin](https://github.com/steirico/kirby-plugin-custom-add-fields). That’s how I’m getting the start/end dates early on (in the create page dialog). That works as expected, but I just don’t know how to grab the parent’s title.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 24, 2020, 9:19pm UTC](https://forum.getkirby.com/t/get-parent-page-title-from-model/20142/5 "2020-11-24T21:19:11Z")

</div>

Is the parent dynamic or always the same page? If so, you can hardcode the page

```auto
$props['content']['title'] = page('parentpagepath')->title() .' '. $start_date .' - ' . $end_date;

```

The `create` method is a static method, so that mean that inside this static method we cannot use `$this`, because that only works within non-static methods.

---

<div class="post-metadata">

**Author:** ![thewebprojects](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thewebprojects/32/10107_2.png) [@thewebprojects](https://forum.getkirby.com/u/thewebprojects)\
**Post date:** [November 24, 2020, 9:23pm UTC](https://forum.getkirby.com/t/get-parent-page-title-from-model/20142/6 "2020-11-24T21:23:46Z")

</div>

> [@texnixe](#):
>
> Is the parent dynamic or always the same page? If so, you can hardcode the page

The parent could be different.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 24, 2020, 9:23pm UTC](https://forum.getkirby.com/t/get-parent-page-title-from-model/20142/7 "2020-11-24T21:23:46Z")

</div>

Another option would be to create a temporary page object from the props:

```auto
public static function create(array $props)
{
    $start_date = strtotime($props['content']['camp_start_date']); 
    $start_date = date('m/d/Y', $start_date);
	$end_date = strtotime($props['content']['camp_end_date']); 
	$end_date = date('m/d/Y', $end_date);
    $props['slug'] = uniqid();
    $props['draft'] = false;
    $page = Page::factory($props);

    $props['content']['title'] = $page->parent()->title() . ' ' . $start_date . ' - ' . $end_date;

    return parent::create($props);
}

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 24, 2020, 9:24pm UTC](https://forum.getkirby.com/t/get-parent-page-title-from-model/20142/8 "2020-11-24T21:24:07Z")

</div>

See above.

---

<div class="post-metadata">

**Author:** ![thewebprojects](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thewebprojects/32/10107_2.png) [@thewebprojects](https://forum.getkirby.com/u/thewebprojects)\
**Post date:** [November 24, 2020, 9:27pm UTC](https://forum.getkirby.com/t/get-parent-page-title-from-model/20142/9 "2020-11-24T21:27:47Z")

</div>

Amazing! Yes. This works, but I’m not sure why.

Where are the nitty gritty details on all this stuff? Admittedly I find myself copying and pasting and wishing for the best.

Thanks @texnixe

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 24, 2020, 9:38pm UTC](https://forum.getkirby.com/t/get-parent-page-title-from-model/20142/10 "2020-11-24T21:38:34Z")

</div>

The problem is that to refer to call a dynamic method of a class inside a static method, you have to actually create a new instance of this class. You cannot use `self` in to call a non-static method.

---

<div class="post-metadata">

**Author:** ![thewebprojects](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thewebprojects/32/10107_2.png) [@thewebprojects](https://forum.getkirby.com/u/thewebprojects)\
**Post date:** [November 24, 2020, 10:43pm UTC](https://forum.getkirby.com/t/get-parent-page-title-from-model/20142/11 "2020-11-24T22:43:38Z")

</div>

I see. Thanks!

---

<div class="post-metadata">

**Author:** ![thewebprojects](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thewebprojects/32/10107_2.png) [@thewebprojects](https://forum.getkirby.com/u/thewebprojects)\
**Post date:** [November 25, 2020, 4:03pm UTC](https://forum.getkirby.com/t/get-parent-page-title-from-model/20142/12 "2020-11-25T16:03:02Z")

</div>

Hello again. I’m using the above model to dynamically build a title at the creation of the page. What if I also wanted this code to fire at the save of the page. Ideally I want the start date, end date and location to be in the title. If the user changes any of those bits of data I’ll want the title to be updated. Any insight?

---

<div class="post-metadata">

**Author:** ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)\
**Post date:** [November 25, 2020, 4:15pm UTC](https://forum.getkirby.com/t/get-parent-page-title-from-model/20142/13 "2020-11-25T16:15:35Z")

</div>

For this, you would have to overwrite the `update()` page method and modify the input, see PageActions.php for the signature of the method.

---

<div class="post-metadata">

**Author:** ![thewebprojects](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thewebprojects/32/10107_2.png) [@thewebprojects](https://forum.getkirby.com/u/thewebprojects)\
**Post date:** [November 25, 2020, 4:27pm UTC](https://forum.getkirby.com/t/get-parent-page-title-from-model/20142/14 "2020-11-25T16:27:02Z")

</div>

I’m a bit over my head with this task. What might that look like? I do not have a firm (or even loose grasp of OOP). I can’t quite find documentation on how to achieve these feats with models.

---

<div class="post-metadata">

**Author:** ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)\
**Post date:** [November 25, 2020, 4:31pm UTC](https://forum.getkirby.com/t/get-parent-page-title-from-model/20142/15 "2020-11-25T16:31:35Z")

</div>

I have yet to test this myself first because I’m not sure if the title is even part of $input in this method, maybe not. In that case, I guess you would have to a hook instead.

---

<div class="post-metadata">

**Author:** ![thewebprojects](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thewebprojects/32/10107_2.png) [@thewebprojects](https://forum.getkirby.com/u/thewebprojects)\
**Post date:** [November 25, 2020, 5:02pm UTC](https://forum.getkirby.com/t/get-parent-page-title-from-model/20142/16 "2020-11-25T17:02:35Z")

</div>

Thanks for the reply @pixelijn

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 25, 2020, 6:40pm UTC](https://forum.getkirby.com/t/get-parent-page-title-from-model/20142/17 "2020-11-25T18:40:59Z")

</div>

This works, kind of:

```auto
public function update(array $input = null, string $language = null, bool $validate = false)
{
  
    if ($this->isDraft() === true) {
        $validate = false;
    }

    $page = parent::update($input, $language, $validate);
    $page = $page->changeTitle('Hello World');

    // if num is created from page content, update num on content update
    if ($page->isListed() === true && in_array($page->blueprint()->num(), ['zero', 'default']) === false) {
        $page = $page->changeNum($page->createNum());
    }

    return $page;
}

```

Or adding the new title to the `$input` array instead:

```auto
public function update(array $input = null, string $language = null, bool $validate = false)
{
  
   $input['title'] = 'Hallo Nachwelt';

   return parent::update($input, $language, $validate);
}

```

But the downside of this approache is that the title is not updated in real time but only after the page is reloaded. Which is the same as when you use a hook.

---

<div class="post-metadata">

**Author:** ![thewebprojects](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thewebprojects/32/10107_2.png) [@thewebprojects](https://forum.getkirby.com/u/thewebprojects)\
**Post date:** [November 25, 2020, 6:52pm UTC](https://forum.getkirby.com/t/get-parent-page-title-from-model/20142/18 "2020-11-25T18:52:22Z")

</div>

@texnixe, thank you for providing additional information on this.
