summaryrefslogtreecommitdiffstats
path: root/src/Plugin/Block/FloatingMenuBlock.php
blob: aac1b0a1f31ebecbec1b24788696c983c58a4afa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php

namespace Drupal\floating_menu\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;

/**
 * Provides a menu block.
 *
 * @Block(
 *   id = "floating_menu_block",
 *   admin_label = @Translation("Floating Menu"),
 * )
 */
class FloatingMenuBlock extends BlockBase implements BlockPluginInterface {

  /**
   * {@inheritdoc}
   */
  public function build() {
    $config = $this->getConfiguration();

    $menu_item_variables = [];
    for ($i = 0; $i < 5; $i++) {

      $untranslated_url = $config['menu_item'][$i]['menu_item_target_url'];
      if (!empty($untranslated_url) && substr($untranslated_url, 0, 1) == '/') {
        $langManager = \Drupal::service('language_manager');
        $path = \Drupal::service('path.alias_manager')->getPathByAlias($untranslated_url, 'fi');
        $translated_url = \Drupal::service('path.alias_manager')->getAliasByPath($path, $langManager->getCurrentLanguage()->getId());
        if ($langManager->getCurrentLanguage()->getId() != $langManager->getDefaultLanguage()->getId()) {
          $translated_url = '/' . $langManager->getCurrentLanguage()->getId() . $translated_url;
        }
      }
      else {
        $translated_url = $untranslated_url;
      }

      if (!empty($config['menu_item'][$i]['menu_item_icon_url'])) {
        $menu_item_variables[] = [
          'popup_html' => [
            '#markup' => $config['menu_item'][$i]['menu_item_popup_html']['value'],
          ],
          'url' => $translated_url,
          'icon_url' => $config['menu_item'][$i]['menu_item_icon_url'],
        ];
      }
    }
    return [
      '#theme' => 'floating_menu_block',
      '#attached' => [
        'library' => [
          'floating_menu/floating-menu',
        ],
      ],
      '#menu_items' => $menu_item_variables,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function blockForm($form, FormStateInterface $form_state) {
    $form = parent::blockForm($form, $form_state);
    $config = $this->getConfiguration();

    $form['menu_items'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Menu Items'),
      '#prefix' => '<div id="menu-item-fieldset-wrapper">',
      '#suffix' => '</div>',
    ];

    for ($i = 0; $i < 5; $i++) {
      $form['menu_items']['menu_item_' . $i] = [
        '#type' => 'fieldset',
        '#title' => $this->t('Menu Item') . ' ' . ($i + 1),
        '#tree' => TRUE,
      ];
      $form['menu_items']['menu_item_' . $i]['menu_item_popup_html'] = [
        '#type' => 'text_format',
        '#format' => 'full_html',
        '#title' => $this->t('Menu Item Popup HTML'),
        '#default_value' => $config['menu_item'][$i]['menu_item_popup_html']['value'],
      ];
      $form['menu_items']['menu_item_' . $i]['menu_item_target_url'] = [
        '#type' => 'textfield',
        '#title' => $this->t('Menu Item Target URL'),
        '#default_value' => $config['menu_item'][$i]['menu_item_target_url'],
      ];
      $form['menu_items']['menu_item_' . $i]['menu_item_icon'] = array(
        '#type' => 'managed_file',
        '#title' => $this->t('Menu Item Icon'),
        '#upload_validators' => [
          'file_validate_is_image' => [],
          'file_validate_extensions' => ['jpg jpeg png gif'],
        ],
        '#upload_location' => 'public://',
        '#default_value' => [$config['menu_item'][$i]['menu_item_icon_file_id']],
      );
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state) {
    $form_state_values = $form_state->getValues();
    $this->configuration['menu_item'] = [];
    foreach ($form_state_values['menu_items'] as $value) {
      $file = File::load($value['menu_item_icon'][0]);
      if (!empty($file)) {
        $file->setPermanent();
        $file->save();
        $image_url = file_create_url($file->getFileUri());
      }
      else {
        $image_url = NULL;
      }
      $this->configuration['menu_item'][] = [
        'menu_item_popup_html' => $value['menu_item_popup_html'],
        'menu_item_target_url' => $value['menu_item_target_url'],
        'menu_item_icon_file_id' => intval($value['menu_item_icon'][0]),
        'menu_item_icon_url' => $image_url,
      ];
    }
  }
  
}