layoutcomponents-8.x-1.14-beta2/modules/lc_simple_countdown/lc_simple_countdown.module
modules/lc_simple_countdown/lc_simple_countdown.module
<?php
/**
* @file
* LC Simple countdown module file.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_theme().
*/
function lc_simple_countdown_theme($existing, $type, $theme, $path) {
return [
'layoutcomponents_block_content__simple_countdown' => [
'base hook' => 'block',
],
];
}
/**
* Implements hook_help().
*/
function lc_simple_countdown_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Create help page.
case 'help.page.lc_simple_countdown':
$module_handler = \Drupal::service('module_handler');
$module_path = $module_handler->getModule('lc_simple_countdown')->getPath();
$file = $module_path . '/README.md';
if (!file_exists($file)) {
return '';
}
// Get content from file.
$reader = file_get_contents($file);
// Return "clean" content.
return preg_replace("/\r\n|\n|\r/", "<br>", $reader);
}
}
/**
* Implements hook_page_attachments().
*/
function lc_simple_countdown_page_attachments(&$page) {
$page['#attached']['library'][] = 'lc_simple_countdown/lc_simple_countdown';
}
/**
* Implements hook_preprocess_HOOK().
*/
function lc_simple_countdown_preprocess_block(&$variables) {
if ($variables['base_plugin_id'] != 'inline_block') {
return;
}
if ($variables['derivative_plugin_id'] != 'simple_countdown') {
return;
}
$helper = \Drupal::service("plugin.manager.layoutcomponents_layouts");
/** @var \Drupal\block_content\Entity\BlockContent $block */
$block = $variables['content']['#block_content'];
$block_id = str_replace(' ', '_', $block->uuid());
// Default.
$date_styles = ["text-align: center"];
// General.
$date = $block->get('field_sc_date')->getString();
// Sizing.
$date_size = $block->get('field_sc_size')->getString();
// Color.
$date_color = $block->get('field_sc_color')->getString();
if (!empty($date_size)) {
$date_styles[] = "font-size: " . $date_size . "px";
}
if (!empty($date_color)) {
$rgba = $helper->hexToRgba($date_color, 1);
$date_styles[] = "color: $rgba";
}
$variables['content_attributes']['class'] = [
'lc-inline_block_' . $block_id . '-countdown-container-edit',
];
// Create new date array.
$date = [
'#type' => "html_tag",
'#tag' => 'div',
'#attributes' => [
'id' => 'lc-simple_countdown-' . $block_id,
'countdown' => $date,
'class' => [
'lc-inline_block_' . $block_id . '-countdown-date-edit',
],
'style' => implode(";", $date_styles),
],
'#value' => "",
];
// Store new date.
$variables['content']['field_sc_date'] = array_merge($variables['content']['field_sc_date'], $date);
}
/**
* Implements hook_inline_entity_form_entity_form_alter().
*/
function lc_simple_countdown_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {
if ($entity_form['#bundle'] == 'simple_countdown') {
if (!array_key_exists('#default_value', $entity_form) || !isset($entity_form['#default_value'])) {
return;
}
/** @var \Drupal\block_content\Entity\BlockContent $block */
$block = $entity_form['#default_value'];
$block_id = str_replace(" ", "_", $block->uuid());
_lc_simple_countdown_form_alter($entity_form, $block_id);
}
}
/**
* Implements hook_block_type_form_alter().
*/
function lc_simple_countdown_block_type_form_alter(array &$form, FormStateInterface &$form_state, $block_type) {
if ($block_type == "simple_countdown") {
if (!array_key_exists('#block', $form)) {
return;
}
/** @var \Drupal\block_content\Entity\BlockContent $block */
$block = $form['#block'];
$block_id = str_replace(" ", "_", $block->uuid());
_lc_simple_countdown_form_alter($form, $block_id);
}
}
/**
* Change the elements with LayoutComponents Api.
*
* @param array $form
* The array with the form.
* @param string $block_id
* The id of the block.
*/
function _lc_simple_countdown_form_alter(array &$form, $block_id) {
/** @var \Drupal\layoutcomponents\Api\Component $lcApi */
$lcApi = Drupal::service('layoutcomponents.apiComponent');
// LC date.
$date = $form['field_sc_date']['widget'][0];
$form['field_sc_date']['widget'][0] = $lcApi->getComponentElement(
[
'no_lc' => TRUE,
],
$date
);
// LC size.
$size = $form['field_sc_size']['widget'][0]['value'];
$form['field_sc_size']['widget'][0]['value'] = $lcApi->getComponentElement(
[
'id' => 'block_' . $block_id . '-countdown-date',
'type' => 'style',
'style' => 'font-size',
'element' => 'slider',
'class' => 'size',
],
$size
);
// LC color.
$text_color = $form['field_sc_color']['widget'][0];
$form['field_sc_color']['widget'][0] = $lcApi->getComponentElement(
[
'id' => 'block_' . $block_id . '-countdown-date',
'type' => 'style',
'style' => 'color',
'element' => 'color',
'class' => 'text_color',
],
$text_color
);
}
