SimpleMarkdown-omeka-module/src/Site/BlockLayout/SimpleMDText.php
2021-09-18 22:10:50 +02:00

90 lines
2.4 KiB
PHP

<?php
namespace SimpleMDText\Site\BlockLayout;
use Omeka\Api\Representation\SiteRepresentation;
use Omeka\Api\Representation\SitePageRepresentation;
use Omeka\Api\Representation\SitePageBlockRepresentation;
use Omeka\Site\BlockLayout\AbstractBlockLayout;
use Laminas\View\Renderer\PhpRenderer;
use Laminas\Form\FormElementManager;
use SimpleMDText\Form\SimpleMDTextBlockForm;
$list_of_includes = get_included_files();
$parsedown_included=false;
foreach ($list_of_includes as $file_path) {
if (false !== strpos($file_path, "Parsedown.php")) {
$parsedown_included=true;
}
}
if ($parsedown_included == false){
require_once(dirname(__DIR__, 3) . '/vendor/autoload.php');
}
use Parsedown;
class SimpleMDText extends AbstractBlockLayout
{
/**
* @var FormElementManager
*/
protected $formElementManager;
/**
* @var array
*/
protected $defaultSettings = [];
/**
* @param FormElementManager $formElementManager
* @param array $defaultSettings
*/
public function __construct(FormElementManager $formElementManager, array $defaultSettings)
{
$this->formElementManager = $formElementManager;
$this->defaultSettings = $defaultSettings;
}
public function getLabel() {
return 'Simple Text';
}
public function form(PhpRenderer $view,
SiteRepresentation $site,
SitePageRepresentation $page = null,
SitePageBlockRepresentation $block = null
) {
$form = $this->formElementManager->get(SimpleMDTextBlockForm::class);
$data = $block
? $block->data() + $this->defaultSettings
: $this->defaultSettings;
$form->setData([
'o:block[__blockIndex__][o:data][markdown_text]' => $data['markdown_text'],
]);
$form->prepare();
$html = '';
$html .= '<a href="#" class="collapse" aria-label="collapse"><h4>' . $view->translate('Options'). '</h4></a>';
$html .= '<div class="collapsible" style="padding-top:6px;">';
$html .= $view->formCollection($form);
$html .= '</div>';
return $html;
}
public function render(PhpRenderer $view, SitePageBlockRepresentation $block)
{
$parsedown = new Parsedown();
return $view->partial('common/block-layout/simpleMDText', [
'html' => $parsedown->text($block->dataValue('markdown_text'))
]);
}
public function getFulltextText(PhpRenderer $view, SitePageBlockRepresentation $block)
{
return strip_tags($this->render($view, $block));
}
}