podcast_publisher-1.0.0-alpha3/podcast_publisher.module
podcast_publisher.module
<?php
/**
* @file
* Provides a podcast entity type.
*/
use Drupal\Core\Render\Element;
use Drupal\Core\Template\Attribute;
use Drupal\user\UserInterface;
/**
* Implements hook_theme().
*/
function podcast_publisher_theme() {
return [
'podcast' => [
'render element' => 'elements',
],
'podcast_episode' => [
'render element' => 'elements',
],
'podcast_publisher_nested_element' => [
'variables' => [
'key' => NULL,
'attributes' => [],
'value' => NULL,
],
],
];
}
/**
* Prepares variables for podcast templates.
*
* Default template: podcast.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the podcast information and any
* fields attached to the entity.
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_podcast(array &$variables) {
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}
/**
* Implements hook_preprocess().
*/
function podcast_publisher_preprocess_views_view_podcast_rss(&$variables) {
\Drupal::moduleHandler()->loadInclude('views', 'inc', 'views.theme');
foreach ($variables['view']->style_plugin->channel_elements as &$element) {
if (isset($element['attributes']) && is_array($element['attributes'])) {
$element['attributes'] = new Attribute($element['attributes']);
}
}
template_preprocess_views_view_rss($variables);
$view = $variables['view'];
/** @var \Drupal\podcast_publisher\Plugin\views\style\PodcastFeed $style */
$style = $view->style_plugin;
$variables['title'] = $style->getTitle();
}
/**
* Prepares variables for podcast episode templates.
*
* Default template: podcast-episode.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the podcast episode information and any
* fields attached to the entity.
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_podcast_episode(array &$variables) {
$variables['view_mode'] = $variables['elements']['#view_mode'];
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}
/**
* Implements hook_user_cancel().
*/
function podcast_publisher_user_cancel($edit, UserInterface $account, $method) {
switch ($method) {
case 'user_cancel_block_unpublish':
// Unpublish podcast episodes.
$storage = \Drupal::entityTypeManager()->getStorage('podcast_episode');
$podcast_episode_ids = $storage->getQuery()
->condition('uid', $account->id())
->condition('status', 1)
->execute();
foreach ($storage->loadMultiple($podcast_episode_ids) as $podcast_episode) {
$podcast_episode->set('status', FALSE);
$podcast_episode->save();
}
break;
case 'user_cancel_reassign':
// Anonymize podcast episodes.
$storage = \Drupal::entityTypeManager()->getStorage('podcast_episode');
$podcast_episode_ids = $storage->getQuery()
->condition('uid', $account->id())
->execute();
foreach ($storage->loadMultiple($podcast_episode_ids) as $podcast_episode) {
$podcast_episode->setOwnerId(0);
$podcast_episode->save();
}
break;
}
}
/**
* Implements hook_ENTITY_TYPE_predelete() for user entities.
*/
function podcast_publisher_user_predelete(UserInterface $account) {
// Delete podcast episodes.
$storage = \Drupal::entityTypeManager()->getStorage('podcast_episode');
$podcast_episode_ids = $storage->getQuery()
->condition('uid', $account->id())
->execute();
$podcast_episodes = $storage->loadMultiple($podcast_episode_ids);
$storage->delete($podcast_episodes);
// Delete old revisions.
$podcast_episode_ids = $storage->getQuery()
->allRevisions()
->condition('uid', $account->id())
->execute();
foreach (array_keys($podcast_episode_ids) as $revision_id) {
$storage->deleteRevision($revision_id);
}
}
/**
* Implements hook_page_attachments().
*/
function podcast_publisher_page_attachments(array &$attachments) {
// Add CSS for the Podcast icon in the toolbar if the user has access to it.
$permissions = ['administer podcast', 'create podcast', 'access podcast overview'];
foreach ($permissions as $permission) {
if (\Drupal::currentUser()->hasPermission($permission) && \Drupal::currentUser()->hasPermission('access toolbar')) {
$attachments['#attached']['library'][] = 'podcast_publisher/drupal.podcast_publisher.toolbar';
break;
}
}
}
