outline-8.x-1.x-dev/outline.module
outline.module
<?php
/**
* @file
* Organize entities in an outline.
*/
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\outline\Entity\Outline;
use Drupal\outline\Entity\Entry;
use Drupal\outline\EntryInterface;
use Drupal\Component\Utility\Unicode;
/**
* Implements hook_help().
*/
function outline_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.outline':
//$field_ui_url = \Drupal::moduleHandler()->moduleExists('field_ui') ? \Drupal::url('help.page', array('name' => 'field_ui')) : '#';
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Outline Entity module allows entities to be organized in outlines.') . '</p>';
$output .= '<p>' . t('For more information, see the <a href=":outline">online documentation for the Outline Entity module</a>.', array(':outline' => 'https://www.drupal.org/documentation/modules/outline/')) . '</p>';
return $output;
}
}
/**
* Entity URI callback.
*/
function outline_entry_uri($entry) {
return new Url('entity.outline_entry.canonical', array(
'outline_entry' => $entry->id(),
));
}
/**
* Implements hook_page_attachments_alter().
*
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
function outline_page_attachments_alter(array &$page) {
$route_match = \Drupal::routeMatch();
if ($route_match->getRouteName() == 'entity.outline_entry.canonical' && ($entry = $route_match->getParameter('outline_entry')) && $entry instanceof EntryInterface) {
foreach ($entry->uriRelationships() as $rel) {
// Set the URI relationships, like canonical.
$page['#attached']['html_head_link'][] = array(
array(
'rel' => $rel,
'href' => $entry->toUrl($rel),
),
TRUE,
);
// Set the entry path as the canonical URL to prevent duplicate content.
if ($rel == 'canonical') {
// Set the non-aliased canonical path as a default shortlink.
$page['#attached']['html_head_link'][] = array(
array(
'rel' => 'shortlink',
'href' => $entry->toUrl($rel, array('alias' => TRUE)),
),
TRUE,
);
}
}
}
}
/**
* Implements hook_theme().
*/
function outline_theme() {
return array(
'outline_entry' => array(
'render element' => 'elements',
),
'outline' => [
'render element' => 'elements',
//'file' => 'outline.theme.inc',
'template' => 'outline',
'variables' => array(
'outline' => NULL,
),
],
);
}
/**
* Generates an array which displays an entry detail page.
*
* @param \Drupal\outline\Entity\Entry $entry
* An outline entry object.
* @param string $view_mode
* View mode; e.g., 'full', 'teaser', etc.
* @param string $langcode
* (optional) A language code to use for rendering. Defaults to the global
* content language of the current request.
*
* @return array
* A $page element suitable for use by drupal_render().
*/
function outline_entry_view(Entry $entry, $view_mode = 'full', $langcode = NULL) {
return entity_view($entry, $view_mode, $langcode);
}
/**
* Constructs a drupal_render() style array from an array of loaded entries.
*
* @param array $entries
* An array of outline entries as returned by Entry::loadMultiple().
* @param string $view_mode
* View mode; e.g., 'full', 'teaser', etc.
* @param string $langcode
* (optional) A language code to use for rendering. Defaults to the global
* content language of the current request.
*
* @return array
* An array in the format expected by drupal_render().
*/
//function outline_entry_view_multiple(array $entries, $view_mode = 'full', $langcode = NULL) {
// //return entity_view_multiple($entries, $view_mode, $langcode);
// return $this->viewMultiple($entities, $view_mode, $langcode);
//}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function outline_theme_suggestions_outline_entry(array $variables) {
$suggestions = array();
/** @var \Drupal\outline\EntryInterface $entry */
$entry = $variables['elements']['#outline_entry'];
$suggestions[] = 'outline_entry__' . $entry->bundle();
$suggestions[] = 'outline_entry__' . $entry->id();
return $suggestions;
}
/**
* Prepares variables for outline entry templates.
*
* Default template: outline-entry.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the outline entry and any
* fields attached to the entry. Properties used:
* - #outline_entry: A \Drupal\outline\EntryInterface object.
* - #view_mode: The current view mode for this outline entry, e.g.
* 'full' or 'teaser'.
* - attributes: HTML attributes for the containing element.
*
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
function template_preprocess_outline_entry(&$variables) {
$variables['view_mode'] = $variables['elements']['#view_mode'];
$variables['outline_entry'] = $variables['elements']['#outline_entry'];
/** @var \Drupal\outline\EntryInterface $entry */
$entry = $variables['outline_entry'];
$variables['url'] = $entry->toUrl();
// We use name here because that is what appears in the UI.
$variables['name'] = $variables['elements']['name'];
unset($variables['elements']['name']);
$variables['page'] = $variables['view_mode'] == 'full' && outline_entry_is_page($entry);
// Helpful $content variable for templates.
$variables['content'] = array();
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}
/**
* Returns whether the current page is the page of the passed-in entry.
*
* @param \Drupal\outline\Entity\Entry $entry
* An outline entry entity.
*/
function outline_entry_is_page(Entry $entry) {
//@TODO is this function named correctly?
if (\Drupal::routeMatch()->getRouteName() == 'entity.outline_entry.canonical' && $page_entry_id = \Drupal::routeMatch()->getRawParameter('outline_entry')) {
return $page_entry_id == $entry->id();
}
return FALSE;
}
/**
* Clear all static cache variables for entries.
*/
function outline_entry_static_reset() {
\Drupal::entityTypeManager()->getStorage('outline_entry')->resetCache();
}
/**
* Clear all static cache variables for outlines.
*
* @param $ids
* An array of ids to reset in the entity cache.
*/
function outline_static_reset(array $ids = NULL) {
\Drupal::entityTypeManager('outline_entry')->resetCache($ids);
}
/**
* Get names for all outlines.
*
* @return array
* A list of existing outline IDs.
*/
function outline_get_names() {
$names = &drupal_static(__FUNCTION__);
if (!isset($names)) {
$names = array();
$config_names = \Drupal::configFactory()->listAll('outline');
foreach ($config_names as $config_name) {
$id = substr($config_name, strlen('outline.'));
$names[$id] = $id;
}
}
return $names;
}
/**
* Try to map a string to an existing entry, as for glossary use.
*
* Provides a case-insensitive and trimmed mapping, to maximize the
* likelihood of a successful match.
*
* @param $name
* Name of the entry to search for.
* @param $outline
* (optional) Outline machine name to limit the search. Defaults to NULL.
*
* @return
* An array of matching entry objects.
*/
function outline_entry_load_multiple_by_name($name, $outline = NULL) {
$values = array('name' => trim($name));
if (isset($outline)) {
$outlines = outline_get_names();
if (isset($outlines[$outline])) {
$values['oid'] = $outline;
}
else {
// Return an empty array when filtering by a non-existing outline.
return array();
}
}
return entity_load_multiple_by_properties('outline_entry', $values);
}
/**
* Load multiple outline entries based on certain conditions.
*
* This function should be used whenever you need to load more than one entry
* from the database. Entries are loaded into memory and will not require
* database access if loaded again during the same page request.
*
* @see entity_load_multiple()
* @see \Drupal\Core\Entity\Query\EntityQueryInterface
*
* @deprecated in Drupal 8.x, will be removed before Drupal 9.0.
* Use \Drupal\outline\Entity\Entry::loadMultiple().
*
* @param array $eids
* (optional) An array of entity IDs. If omitted, all entities are loaded.
*
* @return array
* An array of outline entry entities, indexed by eid. When no results are
* found, an empty array is returned.
*/
function outline_entry_load_multiple(array $eids = NULL) {
return Entry::loadMultiple($eids);
}
/**
* Loads multiple outlines based on certain conditions.
*
* This function should be used whenever you need to load more than one
* outline from the database. Entries are loaded into memory and will not
* require database access if loaded again during the same page request.
*
* @see entity_load_multiple()
*
* @deprecated in Drupal 8.x, will be removed before Drupal 9.0.
* Use \Drupal\outline\Entity\Outline::loadMultiple().
*
* @param array $oids
* (optional) An array of outline IDs. If omitted, all entities are loaded.
*
* @return array
* An array of outline objects, indexed by oid.
*/
function outline_load_multiple(array $oids = NULL) {
return Outline::loadMultiple($oids);
}
/**
* Return the outline outline entity matching an outline ID.
*
* @deprecated in Drupal 8.x, will be removed before Drupal 9.0.
* Use \Drupal\outline\Entity\Outline::load().
*
* @param int $oid
* The outline's ID.
*
* @return \Drupal\outline\Entity\Outline|null
* The outline outline entity, if exists, NULL otherwise. Results are
* statically cached.
*/
function outline_load($oid) {
return Outline::load($oid);
}
/**
* Return the outline entry entity matching an entry ID.
*
* @deprecated in Drupal 8.x, will be removed before Drupal 9.0.
* Use \Drupal\outline\Entity\Entry::load().
*
* @param $eid
* An entry's ID
*
* @return \Drupal\outline\Entity\Entry|null
* An outline entry entity, or NULL if the entry was not found. Results are
* statically cached.
*/
function outline_entry_load($eid) {
if (!is_numeric($eid)) {
return NULL;
}
return Entry::load($eid);
}
/**
* Implodes a list of tags of a specified outline into a string.
*
* @see \Drupal\Component\Utility\Tags::explode()
*/
function outline_implode_tags($tags, $oid = NULL) {
$typed_tags = array();
foreach ($tags as $tag) {
// Extract entries belonging to the outline in question.
if (!isset($oid) || $tag->bundle() == $oid) {
// Make sure we have a completed loaded outline entry.
if ($tag instanceof EntityInterface && $label = $tag->label()) {
// Commas and quotes in tag names are special cases, so encode 'em.
$typed_tags[] = Tags::encode($label);
}
}
}
return implode(', ', $typed_tags);
}
/**
* Title callback for entry pages.
*
* @param \Drupal\outline\Entity\Entry $entry
* An outline entry entity.
*
* @return
* The entry name to be used as the page title.
*/
function outline_entry_title(Entry $entry) {
return $entry->getName();
}
/**
* Implements hook_menu_local_tasks_alter().
*/
function outline_menu_local_tasks_alter(&$data, $route_name) {
unset($data['tabs'][0]['field_ui.fields:overview_entry']);
unset($data['tabs'][0]['field_ui.fields:form_display_overview_entry']);
unset($data['tabs'][0]['field_ui.fields:display_overview_entry']);
}
/**
* Implements hook_js_settings_alter().
*/
function outline_js_settings_alter(array &$settings) {
if (empty($settings['outline']['path'])) {
$module_handler = Drupal::service('module_handler');
$path = $module_handler->getModule('outline')->getPath();
$settings['outline']['path'] = '/' . $path;
}
}
/**
* Implements hook_form_alter().
*/
//function outline_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
// if(substr($form_id, 0, strlen('outline_entry_')) === 'outline_entry_') {
// // $pos = strpos(Url::fromRoute('<current>')->toString(),'render-entry');
// //if($pos !== false && $form_state->getStorage()['outline']['outline']->getHideName()) {
// // unset($form['name']);
// //}
// if($form_state->getStorage()['outline']['outline']->getHideName()) {
// unset($form['name']);
// }
// }
//}
/*
function xoutline_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
if (Unicode::strpos($form['#action'], 'form_action_') === 0) {
$render_array = \Drupal::formBuilder()->renderPlaceholderFormAction();
$action = $render_array['#markup'];
}
else {
$action = $form['#action'];
}
}
*/
//@TODO Did not take index code from taxonomy.module
//@TODO Taxonomy module has taxonomy_term_delete, but corresponding method not found here, e.g. outline_entry_delete
