social_landing_page.module in Open Social 8.6
Same filename and directory in other branches
- 8.9 modules/social_features/social_landing_page/social_landing_page.module
- 8 modules/social_features/social_landing_page/social_landing_page.module
- 8.2 modules/social_features/social_landing_page/social_landing_page.module
- 8.3 modules/social_features/social_landing_page/social_landing_page.module
- 8.4 modules/social_features/social_landing_page/social_landing_page.module
- 8.5 modules/social_features/social_landing_page/social_landing_page.module
- 8.7 modules/social_features/social_landing_page/social_landing_page.module
- 8.8 modules/social_features/social_landing_page/social_landing_page.module
- 10.3.x modules/social_features/social_landing_page/social_landing_page.module
- 10.0.x modules/social_features/social_landing_page/social_landing_page.module
- 10.1.x modules/social_features/social_landing_page/social_landing_page.module
- 10.2.x modules/social_features/social_landing_page/social_landing_page.module
The Social landing page module.
File
modules/social_features/social_landing_page/social_landing_page.moduleView source
<?php
/**
* @file
* The Social landing page module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Template\Attribute;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
use Drupal\node\NodeInterface;
use Drupal\node\Entity\Node;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
/**
* Implements hook_form_alter().
*/
function social_landing_page_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
if (in_array($form_id, [
'node_landing_page_edit_form',
'node_landing_page_form',
])) {
$form['#attached']['library'][] = 'social_landing_page/admin';
}
}
/**
* Implements hook_form_form_ID_alter().
*
* Remove Landing Page option from Search Content filter.
*/
function social_landing_page_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form['#id'] === 'views-exposed-form-search-content-page') {
if (isset($form['type']['#options']['landing_page'])) {
unset($form['type']['#options']['landing_page']);
}
}
}
/**
* Implements hook_theme().
*/
function social_landing_page_theme() {
// Page.
$theme_templates['page__node__landing_page'] = [
'base hook' => 'page',
];
// Node.
$theme_templates['node__landing_page'] = [
'base hook' => 'node',
];
$theme_templates['node__featured'] = [
'base hook' => 'node',
];
$theme_templates['node__event__featured'] = [
'base hook' => 'node',
];
$theme_templates['node__topic__featured'] = [
'base hook' => 'node',
];
$theme_templates['node__page__featured'] = [
'base hook' => 'node',
];
$theme_templates['node__book__featured'] = [
'base hook' => 'node',
];
$theme_templates['node__landing_page__featured'] = [
'base hook' => 'node',
];
// Group.
$theme_templates['group__featured'] = [
'base hook' => 'group',
];
// Profile.
$theme_templates['profile__featured'] = [
'base hook' => 'profile',
];
// Paragraphs.
$theme_templates['paragraph__block__default'] = [
'base hook' => 'paragraph',
];
$theme_templates['paragraph__button__default'] = [
'base hook' => 'paragraph',
];
$theme_templates['paragraph__featured__default'] = [
'base hook' => 'paragraph',
];
$theme_templates['paragraph__featured_item__default'] = [
'base hook' => 'paragraph',
];
$theme_templates['paragraph__featured_items__default'] = [
'base hook' => 'paragraph',
];
$theme_templates['paragraph__hero__default'] = [
'base hook' => 'paragraph',
];
$theme_templates['paragraph__hero_small__default'] = [
'base hook' => 'paragraph',
];
$theme_templates['paragraph__introduction__default'] = [
'base hook' => 'paragraph',
];
$theme_templates['paragraph__section__default'] = [
'base hook' => 'paragraph',
];
// Fields.
$theme_templates['field__paragraph__section'] = [
'base hook' => 'field',
];
// Fields for feature item.
$theme_templates['field__paragraph__field_featured_items'] = [
'base hook' => 'field',
];
// Fields for hero buttons.
$theme_templates['field__paragraph__field_hero_buttons'] = [
'base hook' => 'field',
];
$theme_templates['field__paragraph__field_hero_small_buttons'] = [
'base hook' => 'field',
];
// Views.
$theme_templates['views_view__community_activities'] = [
'base hook' => 'views_view',
];
return $theme_templates;
}
/**
* Implements hook_preprocess_page().
*/
function social_landing_page_preprocess_page(&$variables) {
$nid = \Drupal::routeMatch()
->getRawParameter('node');
// At this point the parameter could also be a simple string of a nid.
// EG: on: /node/%node/enrollments.
if (!is_null($nid) && !is_object($nid)) {
$node = Node::load($nid);
}
// Here we remove class for landing page.
if (isset($variables['node']) && $node
->bundle() === 'landing_page') {
if ($variables['content_attributes'] instanceof Attribute) {
$variables['content_attributes']
->removeClass('layout--with-complementary');
}
else {
$variables['content_attributes'] = new Attribute();
$variables['content_attributes']
->addClass('container');
}
}
}
/**
* Prepares variables for the paragraph.
*/
function social_landing_page_preprocess_paragraph(&$variables) {
/** @var \Drupal\paragraphs\Entity\Paragraph $entity */
$entity = $variables['elements']['#paragraph'];
$bundle = $entity
->bundle();
switch ($bundle) {
case 'hero':
// Add the hero styled image.
$image_style = 'social_landing_hero';
$image_field = "field_{$bundle}_image";
if ($entity
->hasField($image_field) && !empty($entity->{$image_field}->entity)) {
$variables['hero_styled_image_url'] = ImageStyle::load($image_style)
->buildUrl($entity->{$image_field}->entity
->getFileUri());
}
break;
case 'hero_small':
// Add the hero styled image.
$image_style = 'social_landing_hero_small';
$image_field = "field_{$bundle}_image";
if ($entity
->hasField($image_field) && !empty($entity->{$image_field}->entity)) {
$variables['hero_small_styled_image_url'] = ImageStyle::load($image_style)
->buildUrl($entity->{$image_field}->entity
->getFileUri());
}
break;
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function social_landing_page_preprocess_node(&$variables) {
/** @var \Drupal\node\Entity\Node $node */
$node = $variables['node'];
if ($node
->getType() === 'landing_page') {
// If featured we need to do some magic.
if ($variables['view_mode'] === 'featured') {
$hero_image = _social_landing_page_get_hero_image($node);
if (!empty($hero_image)) {
$variables['content']['field_landing_page_image'] = [
'#type' => 'markup',
'#markup' => $hero_image,
];
}
}
// Get current user.
$account = \Drupal::currentUser();
// Add node edit url for management.
if ($node instanceof NodeInterface) {
// Get the current route name to check if the user is on the
// edit or delete page.
$route = \Drupal::routeMatch()
->getRouteName();
if (!in_array($route, [
'entity.node.edit_form',
'entity.node.delete_form',
])) {
if ($node
->access('update', $account)) {
$variables['node_edit_url'] = $node
->toUrl('edit-form')
->toString();
}
}
}
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function social_landing_page_preprocess_field(&$variables) {
if ($variables['field_name'] === 'field_button_link_an' || $variables['field_name'] === 'field_button_link_lu') {
$entity = $variables['element']['#object'];
$button_style = $entity->field_button_style->value;
foreach ($variables['items'] as $key => $value) {
if (isset($variables['items'][$key]['content'])) {
$variables['items'][$key]['content']['#options']['attributes'] = [
'class' => 'btn btn-lg ' . $button_style,
];
}
}
}
}
/**
* Fetches the first available hero section image from a landing page.
*
* @param \Drupal\node\Entity\Node $node
* The landing page.
*
* @return array
* Render array of the image with a link.
*
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
function _social_landing_page_get_hero_image(Node $node) {
// Must be a valid node.
if (!$node instanceof Node || $node
->getType() !== 'landing_page') {
return [];
}
// Loop over the landing page sections of the landing page.
foreach ($node
->get('field_landing_page_section') as $section) {
// Get the referenced paragraph.
$referenced = $section
->getValue();
$paragraph_id = $referenced['target_id'];
// First paragraph is always of type section.
$paragraph_section = Paragraph::load($paragraph_id);
// Get the related paragraph (the one with the actual content)
$section_id = $paragraph_section
->get('field_section_paragraph')->target_id;
$paragraph_content = Paragraph::load($section_id);
// Must be of type hero.
if ($paragraph_content && $paragraph_content
->getType() === 'hero') {
$fid = $paragraph_content
->get('field_hero_image')->target_id;
$file = File::load($fid);
// Check if it's an existing file.
if ($file instanceof File) {
// Build an image render array.
$image = [
'#theme' => 'image_style',
'#style_name' => 'social_featured',
'#uri' => $file
->getFileUri(),
];
// Build a link render array.
$build = [
'#title' => render($image),
'#type' => 'link',
'#url' => $node
->toUrl('canonical'),
];
}
// We immediately return the 1st found hero.
return render($build);
}
}
return [];
}
/**
* Implements hook_social_user_account_header_create_links().
*
* Adds the "Create Landing Page" link to the content creation menu.
*/
function social_landing_page_social_user_account_header_create_links($context) {
return [
'add_landing_page' => [
'#type' => 'link',
'#attributes' => [
'title' => new TranslatableMarkup('Create New Landing Page'),
],
'#title' => new TranslatableMarkup('New Landing Page'),
'#weight' => 400,
] + Url::fromRoute('node.add', [
'node_type' => 'landing_page',
])
->toRenderArray(),
];
}
Functions
Name | Description |
---|---|
social_landing_page_form_alter | Implements hook_form_alter(). |
social_landing_page_form_views_exposed_form_alter | Implements hook_form_form_ID_alter(). |
social_landing_page_preprocess_field | Implements hook_preprocess_HOOK(). |
social_landing_page_preprocess_node | Implements hook_preprocess_HOOK(). |
social_landing_page_preprocess_page | Implements hook_preprocess_page(). |
social_landing_page_preprocess_paragraph | Prepares variables for the paragraph. |
social_landing_page_social_user_account_header_create_links | Implements hook_social_user_account_header_create_links(). |
social_landing_page_theme | Implements hook_theme(). |
_social_landing_page_get_hero_image | Fetches the first available hero section image from a landing page. |