PHP code example of zirak / linkable-dataobjects

1. Go to this page and download the library: Download zirak/linkable-dataobjects library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zirak / linkable-dataobjects example snippets


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();
	 }
 }
}

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');
		}
	}
}