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