Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -586,19 +586,19 @@ To get a menu by its slug, use the syntax below. The menu items will be loaded i

The currently supported menu items are: Pages, Posts, Custom Links and Categories.

Once you'll have instances of `MenuItem` class, if you want to use the original instance (like the original Page or Term, for example), just call the `MenuItem::instance()` method. The `MenuItem` object is just a post with `post_type` equals `nav_menu_item`:
Once you'll have instances of `MenuItem` class, if you want to use the original instance (like the original Page or Term, for example), just access the `MenuItem::object()` relation. The `MenuItem` object is just a post with `post_type` equals `nav_menu_item`:

```php
$menu = Menu::slug('primary')->first();

foreach ($menu->items as $item) {
echo $item->instance()->title; // if it's a Post
echo $item->instance()->name; // if it's a Term
echo $item->instance()->link_text; // if it's a custom link
echo $item->object->title; // if it's a Post
echo $item->object->name; // if it's a Term
echo $item->object->link_text; // if it's a custom link
}
```

The `instance()` method will return the matching object:
`object` will return the matching object:

- `Post` instance for `post` menu item;
- `Page` instance for `page` menu item;
Expand Down
21 changes: 21 additions & 0 deletions src/Laravel/CorcelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
use Corcel\Corcel;
use Corcel\Laravel\Auth\AuthUserProvider;
use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Eloquent\Relations\Relation;
use Corcel\Model\Post;
use Corcel\Model\Page;
use Corcel\Model\CustomLink;
use Corcel\Model\Taxonomy;

/**
* Class CorcelServiceProvider
Expand All @@ -23,6 +28,7 @@ public function boot()
{
$this->publishConfigFile();
$this->registerAuthProvider();
$this->registerMorphMaps();
}

/**
Expand All @@ -47,6 +53,21 @@ private function registerAuthProvider()
}
}

/**
* register morph maps for polymorphic relations
*
* @return void
*/
public function registerMorphMaps()
{
Relation::morphMap([
'post' => Post::class,
'page' => Page::class,
'custom' => CustomLink::class,
'category' => Taxonomy::class,
]);
}

/**
* @return void
*/
Expand Down
22 changes: 20 additions & 2 deletions src/Model/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ class MenuItem extends Post
'category' => Taxonomy::class,
];

/**
* @return Post|Page|CustomLink|Taxonomy
*/
public function object()
{
return $this->morphTo();
}

public function getObjectIdAttribute()
{
return $this->meta->_menu_item_object_id;
}

public function getObjectTypeAttribute()
{
return $this->meta->_menu_item_object;
}

/**
* @return Post|Page|CustomLink|Taxonomy
*/
Expand All @@ -41,13 +59,13 @@ public function parent()
}

/**
* @deprecated deprecated in favor of object()
* @return Post|Page|CustomLink|Taxonomy
*/
public function instance()
{
if ($className = $this->getClassName()) {
return (new $className)->newQuery()
->find($this->meta->_menu_item_object_id);
return $this->object;
}

return null;
Expand Down
13 changes: 12 additions & 1 deletion tests/Unit/Model/MenuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Corcel\Model\MenuItem;
use Corcel\Model\Post;
use Corcel\Model\Taxonomy;
use Corcel\Model\Page;

/**
* Class MenuTest
Expand Down Expand Up @@ -93,7 +94,7 @@ public function it_has_parent_relation()
return $item->meta->_menu_item_object === 'post';
});

$parent = $posts->first()->instance();
$parent = $posts->first()->object;
$child = $posts->last();

$this->assertEquals($parent->ID, $child->parent()->ID);
Expand Down Expand Up @@ -122,7 +123,9 @@ public function it_can_have_custom_links_associated_as_meta()

$this->assertEquals('Foobar', $item->post_title);
$this->assertEquals('Foobar', $item->instance()->link_text);
$this->assertEquals('Foobar', $item->object->link_text);
$this->assertEquals('http://example.com', $item->meta->_menu_item_url);
$this->assertEquals('http://example.com', $item->object->url);
$this->assertEquals('http://example.com', $item->instance()->url);
}

Expand All @@ -138,6 +141,8 @@ public function it_can_have_pages()
});

$pages->each(function (MenuItem $item) {
$this->assertEquals('page', $item->object_type);
$this->assertInstanceOf(Page::class, $item->object);
$this->assertEquals("page-title", $item->instance()->post_title);
$this->assertEquals("page-content", $item->instance()->post_content);
});
Expand All @@ -155,6 +160,8 @@ public function it_can_have_posts()
});

$posts->each(function (MenuItem $item) {
$this->assertEquals('post', $item->object_type);
$this->assertInstanceOf(Post::class, $item->object);
$this->assertEquals("post-title", $item->instance()->title);
$this->assertEquals("post-content", $item->instance()->content);
});
Expand All @@ -172,6 +179,8 @@ public function it_can_have_custom_links()
});

$posts->each(function (MenuItem $item) {
$this->assertEquals('custom', $item->object_type);
$this->assertInstanceOf(CustomLink::class, $item->object);
$this->assertEquals("http://example.com", $item->instance()->url);
$this->assertEquals("custom-link-text", $item->instance()->link_text);
});
Expand All @@ -189,6 +198,8 @@ public function it_can_have_categories()
});

$posts->each(function (MenuItem $item) {
$this->assertEquals('category', $item->object_type);
$this->assertInstanceOf(Taxonomy::class, $item->object);
$this->assertEquals("Bar", $item->instance()->name);
$this->assertEquals("bar", $item->instance()->slug);
});
Expand Down