zirak/widget-pages-extension 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

zirak/widget-pages-extension

Composer 安装命令:

composer require zirak/widget-pages-extension

包简介

Freely inspired to burnbright/silverstripe-widgetpages, add a Widget's Gridfield to the extended pages

README 文档

README

Freely inspired to burnbright/silverstripe-widgetpages, it adds Widget's Gridfields to the extended pages or DataObjects. Widgets Pages Extension is an enhancement for the actual widget module (http://addons.silverstripe.org/add-ons/silverstripe/widgets).

Introduction

This module is a workaround for an old and annoying widget module bug: silverstripe/silverstripe-widgets#20 It's also a Proof of Concept for an alternative way to manage widget through many_many relationsiph rather than the actual has_many relationsiph. Extending the widget behaviour with this module you can be able to link existing widgets instead of rewrite them again. Widgets are sortable inside their WidgetArea. Also DataObjects can have their widges.

Requirements

  • SilverStripe 3.1 or 3.2

Installation

Install the module through composer:

composer zirak/widget-pages-extension

Extend the desired pages through the following yaml:

:::yml
Page:
  extensions:
    - WidgetPage

Define the WidgetAreas in the $has_one relationship, and specify which $allowed_widgets are ok for this page type

:::php
class Page extends SiteTree {

	private static $db = array(
	);
	private static $has_one = array(
			'HeaderBar' => 'WidgetArea',
			'FooterBar' => 'WidgetArea'
	);
	private static $allowed_widgets = array(
			'StandardWidget',
			'TwitterWidget',
			'FacebookWidget'
	);
}

Run a dev/build, and adjust your templates to include the resulting WidgetArea view by calling $WidgetArea function passing it the WidgetArea name as parameter. It will loops through all its widgets.

:::HTML
<div class="typography">
	<h2>HeaderBar</h2>
	<% loop $WidgetArea(HeaderBar) %>
		$WidgetHolder
	<% end_loop %>
</div>

<div class="typography">
	<h2>FooterBar</h2>
	<% loop $WidgetArea(FooterBar) %>
		$WidgetHolder
	<% end_loop %>
</div>

To enable WidgetAreas in backend remove the check from "Inherit Sidebar From Parent" and save the page. The module will create the WidgetAreas and you're now able to start populating them. If you leave the flag on the page will search Widget in its Parent since it find the SiteTree root, then it stops and return a void WidgetArea.

After installing read carefully the ISSUE section at the end of this document. I'm working on a solution but it's not so easy (PR are welcome).

Installing a widget

See widget module docs (http://addons.silverstripe.org/add-ons/silverstripe/widgets).

Adding widgets to other pages

You have to do a couple things to get a Widget to work on a page.

  • Install the Widgets Pages Extension module, see above.
  • Add one or more WidgetArea field to your Page.
  • run dev/build?flush=all

mysite/code/Page.php

:::php
class Page extends SiteTree {

	private static $db = array(
	);
	// Add 4 WidgetAreas
	private static $has_one = array(
			'HeaderBar' => 'WidgetArea',
			'SidebarBar' => 'WidgetArea'
			'CenterWidgetArea' => 'WidgetArea'
			'FooterBar' => 'WidgetArea'
	);
	private static $allowed_widgets = array(
			'StandardWidget',
			'TwitterWidget',
			'FacebookWidget'
	);
}

In this case, you need to alter your templates to loop over $WidgetArea(HeaderBar), $WidgetArea(SidebarBar), $WidgetArea(CenterWidgetArea) and $WidgetArea(FooterBar).

Writing your own widgets

See widget module docs (http://addons.silverstripe.org/add-ons/silverstripe/widgets).

Adding widgets to DataObjects

A DataObject can be renderd as a page, it just need a Route and a Controller. With Widgets Pages Extension also DataObjects can have their Widgets. A sample is following:

The DataObject

mysite/code/DoSurfboard.php

:::php	
class DoSurfboard extends DataObject {

	private static $db = array(
			'Name' => 'Varchar',
			'Color' => 'Varchar'
	);
	private static $has_one = array(
			'LeftSidebar' => 'WidgetArea'
	);
	private static $summary_fields = array (
			'Name',
			'Color'
	);

}

Adding Widgets Pages Extension to DataObject

mysite/code/_config/extensions.yml

:::yml
---
Name: my-extensions
---
DoSurfboard:
	extensions:
		- WidgetDataObject

The route

mysite/code/_config/routes.yml

:::yml
---
Name: myroutes
After: framework/routes#coreroutes
---
Director:
		rules:
				'surfboard//$ID!': 'ShowSurfboard'

The Controller

mysite/code/Controllers/ShowSurfboard.php

:::php	
class ShowSurfboard extends ContentController {

	private static $url_handlers = array('$ID!' => 'handleAction');

	public function index(SS_HTTPRequest $req) {
		$id = $req->param('ID');

		// Use theme from the site config
		if (($config = SiteConfig::current_site_config()) && $config->Theme) {
			SSViewer::set_theme($config->Theme);
		}
		$themedir = $_SERVER['DOCUMENT_ROOT'] . '/' . SSViewer::get_theme_folder() . '/templates/';

		$surfboard = DataObject::get_by_id('DoSurfboard', $id);

		if ($surfboard) {
			//Return our $Data array to use on the page
			$Data = array('DoSurfboard' => $surfboard);
			$this->Customise($Data);
			return $this->renderWith($themedir . 'ShowSurfboard.ss');
		} else {
			//Not found
			return $this->httpError(404, 'Not found');
		}
	}
}

The Template

themes/theme-name/templates/ShowSurfboard.ss

<% with DoSurfboard %>
<h3>$Name</h3>
<p>$Color</p>
<div class="typography">
	<h2>LeftSidebar</h2>
	<% loop $WidgetArea(LeftSidebar) %>
		$WidgetHolder
	<% end_loop %>
</div>
<% end_with %>

TODO

  • Move $InheritSideBar in pages, in order to have the abilty to inherit only some widget area
  • Fix the error listed below in ISSUE section, maybe with a task that re-publish every published page

ISSUE

After installing this module you need to re-publish existing pages where you want to use widgets, because of UnsavedRationList. If you don't re-publish the pages you will get the following error: Error at framework/model/UnsavedRelationList.php line 307: Uncaught LogicException: byID can't be called on an UnsavedRelationList.

zirak/widget-pages-extension 适用场景与选型建议

zirak/widget-pages-extension 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.83k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2013 年 12 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「silverstripe」 「widgets」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 zirak/widget-pages-extension 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 zirak/widget-pages-extension 我们能提供哪些服务?
定制开发 / 二次开发

基于 zirak/widget-pages-extension 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 5.83k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 10
  • 依赖项目数: 2
  • 推荐数: 4

GitHub 信息

  • Stars: 6
  • Watchers: 3
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0
  • 更新时间: 2013-12-12