Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
WedgeSama committed Oct 27, 2015
0 parents commit 2aec6c1
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Extension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/*
* This file is part of the ws/content-type-menu-extension package.
*
* (c) Benjamin Georgeault <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* "Virtual" Extension.php file. It is use only for local extension. It add a PSR-4 autoloader for the extension.
*
* @see http://www.php-fig.org/psr/psr-4/examples/
*/
spl_autoload_register(function ($class) {
$prefix = 'Bolt\\Extension\\WS\\ContentTypeMenuExtension\\';
$base_dir = __DIR__ . '/src/';

$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return;
}

$relative_class = substr($class, $len);
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';

if (file_exists($file)) {
require $file;
}
});
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Benjamin Georgeault

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Content type in menu for Bolt
=============================

This [Bolt](https://bolt.cm/) extension provide an easy way to add content type in menus.

Requirements
------------

* bolt 2.*

Usage
-----

### Menu definition

```yaml
main:
- title: 'This is the first menu item.'
class: first
path: homepage
label: Home
- label: Foo
contenttype: foos
contenttype_params: # optional
order: title
id: '!1'
...
```

`contenttype_params` use same keys as [Fetching content](https://docs.bolt.cm/content-fetching). Use `where` causes
directly in the array.

TODO
----
- Custom title
- Custom alt
- Custom class
- Custom label?

License
-------

This library is release under [MIT license](LICENSE).
27 changes: 27 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "ws/content-type-menu-extension",
"description": "Provide an easy way to add content type in menus.",
"type": "bolt-extension",
"license": "MIT",
"authors": [
{
"name": "Benjamin Georgeault",
"email": "[email protected]"
}
],
"keywords": [
"menu",
"content type"
],
"require": {
"bolt/bolt": ">=2.0.0,<3.0.0"
},
"autoload": {
"files": [
"init.php"
],
"psr-4": {
"Bolt\\Extension\\WS\\ContentTypeMenuExtension\\": "src"
}
}
}
13 changes: 13 additions & 0 deletions init.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/*
* This file is part of the ws/content-type-menu-extension package.
*
* (c) Benjamin Georgeault <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Bolt\Extension\WS\ContentTypeMenuExtension\Extension;

$app['extensions']->register(new Extension($app));
100 changes: 100 additions & 0 deletions src/Extension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/*
* This file is part of the ws/content-type-menu-extension package.
*
* (c) Benjamin Georgeault <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Bolt\Extension\WS\ContentTypeMenuExtension;

use Bolt\BaseExtension;

/**
* Extension
*
* @author Benjamin Georgeault <[email protected]>
*/
class Extension extends BaseExtension
{
/**
* {@inheritdoc}
*/
public function initialize()
{
$menus = $this->app['config']->get('menu');

$updateMenus = array();
foreach ($menus as $name => $items) {
$updateMenus[$name] = $this->parseItems($items);
}

$this->app['config']->set('menu', $updateMenus);
}

/**
* {@inheritdoc}
*/
public function getName()
{
return "WSContentTypeMenuExtension";
}

/**
* Apply contenttype to a menu/submenu tree.
*
* @param array $items
* @return array
*/
protected function parseItems(array $items)
{
$parsedItems = array();
foreach ($items as $item) {
if (array_key_exists('contenttype', $item)) {
$params = array_key_exists('contenttype_params', $item)?$item['contenttype_params']:array();

$parsedItems[] = array_merge($item, array(
'submenu' => $this->getContentItems($item['contenttype'], $params),
));
} else if (array_key_exists('submenu', $item)) {
$parsedItems[] = array_merge($item, array(
'submenu' => $this->parseItems($item['submenu']),
));
} else {
$parsedItems[] = $item;
}
}

return $parsedItems;
}

/**
* Get contents from database.
*
* @param string $contenttype
* @param array $params
* @return array
*/
protected function getContentItems($contenttype, array $params = array())
{
/** @var \Bolt\Storage $storage */
$storage = $this->app['storage'];

$items = array();
$contents = $storage->searchContentType($contenttype, $params);

/**
* @var integer $id
* @var \Bolt\Content $content
*/
foreach ($contents as $id => $content) {
$items[] = array(
'label' => $content->getTitle(),
'path' => $content->getReference(),
);
}

return $items;
}
}

0 comments on commit 2aec6c1

Please sign in to comment.