class LayoutRendererStandard {
var $layout;
var $layout_info;
var $rendered = array();
var $prepared = array();
var $prep_run = FALSE;
var $plugin;
var $admin = FALSE;
var $meta_location = 'standard';
var $menu_response;
var $prefix = '';
var $suffix = '';
protected $page_title = NULL;
function __construct(Layout $layout, array $renderer_plugin) {
$this->plugin = $renderer_plugin;
$layout_info = layout_get_layout_template_info($layout->layout_template);
$this->layout = &$layout;
if (empty($layout_info)) {
throw new LayoutMissingException(t('The layout plugin "@plugin" could not been found to display the "@title" layout.', array('@plugin' => $layout->layout_template, '@title' => $layout->title)));
}
else {
$this->layout_info = $layout_info;
}
}
function prepare() {
if ($this->layout->settings['title_display'] === LAYOUT_TITLE_CUSTOM) {
$this->page_title = t($this->layout->settings['title']);
}
elseif ($this->layout->settings['title_display'] === LAYOUT_TITLE_NONE) {
$this->page_title = FALSE;
}
$this->prepareBlocks($this->layout->content);
$this->prepareRegions($this->layout->positions, $this->layout->settings);
$this->prep_run = TRUE;
}
function prepareBlocks(array $blocks) {
$first = $normal = $last = array();
foreach ($blocks as $uuid => $block) {
if (empty($this->admin)) {
$layout_contexts = $this->layout->getContexts();
$has_contexts = TRUE;
$required_contexts = $block->getRequiredContexts();
if ($required_contexts) {
$has_contexts = $this->layout->hasContexts($required_contexts);
if ($has_contexts) {
$block->setContexts($layout_contexts);
}
}
elseif ($block->conditions) {
$block->setContexts($layout_contexts);
}
if (!$has_contexts || !$block->checkAccess()) {
continue;
}
if ($this->layout->settings['title_display'] === LAYOUT_TITLE_BLOCK) {
if ($block->uuid === $this->layout->settings['title_block']) {
$this->page_title = $block->getTitle();
}
}
}
$block->prepare();
$block_info = layout_get_block_info($block->module, $block->delta);
if (!empty($block_info['render last'])) {
$last[$uuid] = $block;
}
elseif (!empty($block_info['render first'])) {
$first[$uuid] = $block;
}
else {
$normal[$uuid] = $block;
}
}
$this->prepared['blocks'] = $first + $normal + $last;
return $this->prepared['blocks'];
}
function prepareRegions(array $region_block_list, array $settings) {
$default = array(
'style' => !empty($settings['style']) ? $settings['style'] : 'default',
'style settings' => isset($settings['style_settings']['default']) ? $settings['style_settings']['default'] : array(),
);
$regions = array();
if (empty($settings)) {
foreach ($this->layout_info['regions'] as $region_id => $title) {
$blocks = array();
if (isset($region_block_list[$region_id])) {
$blocks = $region_block_list[$region_id];
}
$regions[$region_id] = $default;
$regions[$region_id]['uuids'] = $blocks;
}
}
else {
foreach ($this->layout_info['regions'] as $region_id => $title) {
$blocks = !empty($region_block_list[$region_id]) ? $region_block_list[$region_id] : array();
if (empty($settings[$region_id]['style']) || $settings[$region_id]['style'] == -1) {
$regions[$region_id] = $default;
}
else {
$regions[$region_id]['style'] = $settings[$region_id]['style'];
}
$regions[$region_id]['style settings'] = isset($settings[$region_id]) ? $settings[$region_id] : array();
$regions[$region_id]['uuids'] = $blocks;
}
}
$this->prepared['regions'] = $regions;
return $this->prepared['regions'];
}
function render() {
$this->addMeta();
return $this->renderLayout();
}
function renderLayout() {
if (empty($this->prep_run)) {
$this->prepare();
}
$this->renderBlocks();
if ($this->menu_response) {
return $this->menu_response;
}
$this->renderRegions();
if ($this->page_title) {
backdrop_set_title($this->page_title);
}
module_load_include('inc', 'layout', 'layout.theme');
if (empty($this->layout_info['template'])) {
$theme = 'layout__' . $this->layout->layout_template;
}
else {
$theme = str_replace('-', '_', $this->layout_info['template']);
}
$this->rendered['layout'] = theme($theme, array('content' => $this->rendered['regions'], 'settings' => $this->layout->settings, 'layout' => $this->layout, 'layout_info' => $this->layout_info, 'renderer' => $this, 'admin' => $this->admin));
return $this->prefix . $this->rendered['layout'] . $this->suffix;
}
function addMeta() {
if (!empty($this->layout_info['stylesheets'])) {
foreach ($this->layout_info['stylesheets'] as $media => $file_names) {
foreach ($file_names as $file_name) {
$this->addCss($this->layout_info['path'] . '/' . $file_name, array('media' => $media));
}
}
}
if (!empty($this->layout_info['libraries'])) {
foreach ($this->layout_info['libraries'] as $library) {
$this->addLibrary('layout', $library);
}
}
}
function addCss($filename, $options = array()) {
switch ($this->meta_location) {
case 'standard':
backdrop_add_css($filename, $options);
break;
case 'inline':
$url = base_path() . $filename;
$this->prefix .= '<link rel="stylesheet" href="' . $url . '" />' . "\n";
break;
}
}
function addLibrary($module, $name) {
backdrop_add_library($module, $name);
}
function renderBlocks() {
$this->rendered['blocks'] = array();
foreach ($this->prepared['blocks'] as $uuid => $block) {
if ($block->module === 'system' && $block->delta === 'main') {
$system_content = $this->renderBlock($block);
$response_code = (int) $system_content;
if ($response_code === MENU_NOT_FOUND || $response_code === MENU_ACCESS_DENIED) {
$this->menu_response = $response_code;
return array();
}
}
}
foreach ($this->prepared['blocks'] as $uuid => $block) {
if ($block->module === 'system' && $block->delta === 'main') {
$this->rendered['blocks'][$uuid] = $system_content;
}
elseif ($content = $this->renderBlock($block)) {
$this->rendered['blocks'][$uuid] = $content;
}
}
return $this->rendered['blocks'];
}
function renderBlock($block) {
if (method_exists($block, 'setPageTitle')) {
$page_title = isset($this->page_title) ? $this->page_title : backdrop_get_title();
$block->setPageTitle($page_title);
}
$output = '';
if ($content = $block->getContent()) {
$style = layout_get_style_info($block->style->plugin);
if (is_string($content)) {
$content = array(
'#markup' => $content,
);
}
if (!($block->module === 'system' && $block->delta === 'main')) {
$content['#contextual_links']['layout'] = array('admin/structure/layouts/manage/' . $this->layout->name . '/modal', array($block->uuid));
}
$title = '';
if ($block->settings['title_display'] !== LAYOUT_TITLE_NONE) {
$title = $block->getTitle();
}
$data = array(
'title' => $title,
'content' => $content,
);
backdrop_alter(array('block_view', 'block_view_' . $block->module . '_' . str_replace('-', '_', $block->delta)), $data, $block);
$content = (object) array(
'title' => $data['title'],
'content' => $data['content'],
);
if (isset($style) && isset($style['block theme'])) {
$output = theme($style['block theme'] . '__' . $block->module . '__' . strtr($block->delta, '-', '_'), array('content' => $content, 'layout' => $this->layout, 'block' => $block, 'style' => $style, 'settings' => $block->style->settings));
}
else {
$output = theme('block', array('content' => $content, 'layout' => $this->layout, 'block' => $block));
}
}
return $output;
}
function renderRegions() {
$this->rendered['regions'] = array();
foreach ($this->prepared['regions'] as $region_id => $conf) {
$region_blocks = array();
foreach ($conf['uuids'] as $uuid) {
if (!empty($this->rendered['blocks'][$uuid])) {
$region_blocks[$uuid] = $this->rendered['blocks'][$uuid];
}
}
$this->rendered['regions'][$region_id] = $this->renderRegion($region_id, $region_blocks);
}
return $this->rendered['regions'];
}
function renderRegion($region_id, $blocks) {
$style_settings = $this->prepared['regions'][$region_id]['style settings'];
$tag = isset($style_settings['element']) ? $style_settings['element'] : '';
$classes = array();
if (isset($style_settings['classes'])) {
$classes = explode(' ', $style_settings['classes']);
foreach ($classes as $n => $class) {
$classes[$n] = backdrop_clean_css_identifier($class, array());
}
$classes = array_filter($classes);
}
return theme('layout_region_inner', array('blocks' => $blocks, 'tag' => $tag, 'classes' => $classes));
}
function ensurePageContentBlock() {
$page_content_uuid = NULL;
$default_region = $this->layout_info['default region'];
foreach ($this->layout->content as $uuid => $block) {
if ($block->module === 'system' && $block->delta === 'main') {
$page_content_uuid = $uuid;
}
}
if (!$page_content_uuid) {
$block = layout_create_handler('block', 'system:main');
$block->uuid = 'default';
$this->layout->content['default'] = $block;
$page_content_uuid = $block->uuid;
}
if (!isset($this->layout->positions[$default_region])) {
$this->layout->positions[$default_region] = array();
}
if (!in_array($page_content_uuid, $this->layout->positions[$default_region])) {
array_unshift($this->layout->positions[$default_region], $page_content_uuid);
}
}
}