定制 zirak/linkable-dataobjects 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

zirak/linkable-dataobjects

Composer 安装命令:

composer require zirak/linkable-dataobjects

包简介

This module adds the ability to link DataObjects in TinyMCE

README 文档

README

Linkable DataObjects is a module that permit to link DataObjects from TinyMCE.

Introduction

Pages are not always the better way to implement things. For example site news can grow rapidly and the first side effect would be a big and difficult to manage SiteTree. DataObjects help maintaining things clean and straight, but unfortunately they are not included in frontend search. This module let you insert DataObject in search.

This module add the ability to link DataObjects through TinyMCE, just like internal pages. The linkable DataObjects must obviously implement a Link() function.

Requirements

  • SilverStripe >=3.1 <4.0

Installation

Install the module through composer:

composer require zirak/linkable-dataobjects
composer update

Make the DataObject implement Linkable interface (you need to implement Link(), LinkLabel(), link_shortcode_handler():

class DoNews extends DataObject implements Linkable {

	private static $db = array(
			'Title' => 'Varchar',
			'Subtitle' => 'Varchar',
			'News' => 'HTMLText',
			'Date' => 'Date',
	);
	private static $has_one = array(
			'Page' => 'PghNews'
	);

	/**
	* Link to this DO
	* @return string
	*/
 public function Link() {
	 return $this->Page()->Link() . 'read/' . $this->ID;
 }

 /**
	* Label displayed in "Insert link" menu
	* @return string
	*/
 public static function LinkLabel() {
	 return 'News';
 }

 /**
	* Replace a "[{$class}_link,id=n]" shortcode with a link to the page with the corresponding ID.
	* @param array  $arguments Arguments to the shortcode
	* @param string $content   Content of the returned link (optional)
	* @param object $parser    Specify a parser to parse the content (see {@link ShortCodeParser})
	* @return string anchor Link to the DO page
	*
	* @return string
	*/
 static public function link_shortcode_handler($arguments, $content = null, $parser = null) {
	 if (!isset($arguments['id']) || !is_numeric($arguments['id'])) {
		 return;
	 }

	 $id =  $arguments['id'];
	 $do = DataObject::get_one(__CLASS__, "ID=$id");

	 if (!$do) {
		 $do = DataObject::get_one('ErrorPage', '"ErrorCode" = \'404\'');
		 return $do->Link();
	 }

	 if ($content) {
		 return sprintf('<a href="%s">%s</a>', $do->Link(), $parser->parse($content));
	 } else {
		 return $do->Link();
	 }
 }
}

Here you are a sample page holder, needed to implement the Link() function into the DataObject:

class PghNews extends Page {

	private static $has_many = array(
			'News' => 'DoNews'
	);

	public function getCMSFields() {
		$fields = parent::getCMSFields();

		/* News */
		$gridFieldConfig = GridFieldConfig_RelationEditor::create(100);
		// Remove unlink
		$gridFieldConfig->removeComponentsByType('GridFieldDeleteAction');
		// Add delete
		$gridFieldConfig->addComponents(new GridFieldDeleteAction());
		// Remove autocompleter
		$gridFieldConfig->removeComponentsByType('GridFieldAddExistingAutocompleter');
		$field = new GridField(
						'Faq', 'Faq', $this->News(), $gridFieldConfig
		);
		$fields->addFieldToTab('Root.News', $field);


		return $fields;
	}
}

class PghNews_Controller extends Page_Controller {

	private static $allowed_actions = array(
			'read'
	);

	public function read(SS_HTTPRequest $request) {
		$arguments = $request->allParams();
		$id = $arguments['ID'];

		// Identifico la faq dall'ID
		$Object = DataObject::get_by_id('DoNews', $id);

		if ($Object) {
			//Popolo l'array con il DataObject da visualizzare
			$Data = array($Object->class => $Object);
			$this->data()->Title = $Object->Title;

			$themedir = $_SERVER['DOCUMENT_ROOT'] . '/' . SSViewer::get_theme_folder() . '/templates/';
			$retVal = $this->Customise($Data);
			return $retVal;
		} else {
			//Not found
			return $this->httpError(404, 'Not found');
		}
	}
}

Register your shortcode handlers in your _config.php

:::php
ShortcodeParser::get('default')->register('yournamespace_yourclass_link', array('YourNamespace\YourClass', 'link_shortcode_handler'));

Flush your cache and start linking your DataObjects.

Suggested modules

统计信息

  • 总下载量: 72
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 2
  • Forks: 9
  • 开发语言: JavaScript

其他信息

  • 授权协议: GPL-2
  • 更新时间: 2014-05-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固