Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to menu Attributes, to be able to build Relative or Absolute paths for urls #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
20 changes: 16 additions & 4 deletions menuitemtypes/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,23 @@ public function extendItemForm(Form $form)
'tab' => 'Item',
],
'data[params]' => [
'label' => 'Slug Parameters',
'comment' => 'If a slug uses a parameter such as :slug, enter a value for it here. Enter valid JSON - for example {"slug":"my-page-slug"}',
'label' => 'Slug Parameters / String parameters',
'comment' => 'If a slug uses a parameter such as :slug, enter a value for it here. Enter valid JSON - for example {"slug":"my-page-slug"}. If not valid JSON will be provided - the parameters string will be added as suffix to the url',
'type' => 'text',
'options' => DropDownHelper::instance()->pages(),
'tab' => 'Item',
],
'is_absolute' => [
'label'=> 'Absolute/Relative?',
'span'=> 'left',
'type'=> 'dropdown',
'comment'=> 'How to build the link, to make it relative or absolute',
'options'=> [
0 => 'Relative',
1 => 'Absolute'
],
'tab'=> 'Attributes'
]
], 'primary');
}

Expand Down Expand Up @@ -66,9 +77,10 @@ public function extendItemModel(MenuItem $item)
public function getUrl(MenuItem $item)
{
$params = [];
$absolute = ($item->attributes['is_absolute']*1 == 1)?true:false;
if ( !empty($item->data['params']) )
$params = (array)json_decode($item->data['params']);

return Pg::url(Pg::find($item->master_object_id)->fileName, $params);
return Pg::url(Pg::find($item->master_object_id)->fileName, $params, $absolute);
}
}
}
2 changes: 1 addition & 1 deletion models/Menuitem.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Menuitem extends Model
/**
* @var array Fillable fields
*/
protected $fillable = ['enabled', 'label', 'title_attrib', 'id_attrib', 'class_attrib', 'target_attrib', 'selected_item_id', 'url', 'data', 'master_object_class', 'master_object_id', 'parent_id'];
protected $fillable = ['enabled', 'label', 'title_attrib', 'id_attrib', 'class_attrib', 'target_attrib','is_absolute', 'selected_item_id', 'url', 'data', 'master_object_class', 'master_object_id', 'parent_id'];

public $belongsTo = [
'menu' => ['Flynsarmy\Menu\Models\Menu'],
Expand Down
10 changes: 8 additions & 2 deletions partials/_menuitem.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
<?php
$urlParams = '';
if (!empty($this->data['params'])){
$p = json_decode($this->data['params'],true);
$urlParams = ($p === null)?$this->data['params']:'?'.http_build_query($p);
}

$output = sprintf($settings['before_item'], $this->id, $this->id_attrib, $this->getClassAttrib($settings, $depth), $this->title_attrib);
$output .= $url ?
sprintf($settings['before_url_item_label'], $url, $this->id, $this->id_attrib, $this->getClassAttrib($settings, $depth), $depth, $this->title_attrib, $this->target_attrib) : //<a href="%1$s" title="%2$s" class="title" target="%7$s">
sprintf($settings['before_url_item_label'], $url.$urlParams, $this->id, $this->id_attrib, $this->getClassAttrib($settings, $depth), $depth, $this->title_attrib, $this->target_attrib) : //<a href="%1$s" title="%2$s" class="title" target="%7$s">
sprintf($settings['before_nourl_item_label'], $this->id, $this->id_attrib, $this->getClassAttrib($settings, $depth), $depth, $this->title_attrib); //<span class="title">

$output .= htmlspecialchars($this->label);

$output .= $url ?
sprintf($settings['after_url_item_label'], $url, $this->id, $this->id_attrib, $this->getClassAttrib($settings, $depth), $depth, $this->title_attrib) : //<a href="%1$s" title="%2$s" class="title">
sprintf($settings['after_url_item_label'], $url.$urlParams, $this->id, $this->id_attrib, $this->getClassAttrib($settings, $depth), $depth, $this->title_attrib) : //<a href="%1$s" title="%2$s" class="title">
sprintf($settings['after_nourl_item_label'], $this->id, $this->id_attrib, $this->getClassAttrib($settings, $depth), $depth, $this->title_attrib); //<span class="title">

if ( $child_count || $settings['always_show_before_after_children'] )
Expand Down
25 changes: 25 additions & 0 deletions updates/add_menuitem_is_absolute_field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php namespace Flynsarmy\Menu\Updates;

use Schema;
use October\Rain\Database\Updates\Migration;

class AddMenuitemIsAbsoluteField extends Migration
{

public function up()
{
Schema::table('flynsarmy_menu_menuitems', function($table)
{
$table->string('is_absolute')->default(0);
});
}

public function down()
{
Schema::table('flynsarmy_menu_menuitems', function($table)
{
$table->dropColumn('is_absolute');
});
}

}
5 changes: 4 additions & 1 deletion updates/version.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@
1.0.10: Remove comma in item links
1.0.11:
- Add slug parameters option for Page items
- add_menuitem_data_field.php
- add_menuitem_data_field.php
1.0.12:
- Add Absolute/Relative switcher for menu link
- add_menuitem_is_absolute_field.php