Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
novicell/customeredit / src / Controller / AdminController.php
Size: Mime:
<?php

namespace Drupal\customeredit\Controller;

use Drupal\Component\Transliteration\TransliterationInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
use Drupal\customeredit\CustomerTextsStorage;

class AdminController extends ControllerBase {

  public function textsList($category_id = '') {
    $build = [];
    $category = '';

    /** @var CustomerTextsStorage $storage */
    $storage = \Drupal::service('customeredit.texts.storage');
    $categories = $storage->getCategories();
    /** @var TransliterationInterface $transliteration */
    $transliteration = \Drupal::service('transliteration');
    if (!empty($categories) && $category_id == '') {
      $category = $categories[0];
    }
    $i = 0;
    while (empty($category) && $i < count($categories)) {
      $trans = strtolower(str_replace(' ', '_', $transliteration->transliterate($categories[$i])));
      if ($trans == $category_id) {
        $category = $categories[$i];
      }
      $i++;
    }
    $subcategories = [];
    if (empty($category)) {
      $texts = [];
    } else {
      $texts = $storage->getCategory($category);
      foreach ($texts as $id => $text) {
        if (!in_array($text['subcategory'], $subcategories)) {
          $subcategories[] = $text['subcategory'];
        }
      }
    }
    sort($subcategories);

    foreach ($subcategories as $subcategory) {
      $subcategory_title = (empty($subcategory)) ? $this->t('Miscellaneous') : $subcategory;
      $subcategory_id = $transliteration->transliterate($subcategory_title);
      if ($subcategory != '') {
        $build[$subcategory_id] = array(
          '#type' => 'fieldset',
          '#title' => $subcategory_title,
        );
      }
      $build[$subcategory_id]['list'] = array(
        '#type' => 'table',
        '#header' => array(t('Title'), t('Text'), t('Operations')),
        '#empty' => t('There are no texts yet.'),
        // TableSelect: Injects a first column containing the selection widget into
        // each table row.
        // Note that you also need to set #tableselect on each form submit button
        // that relies on non-empty selection values (see below).
        '#tableselect' => FALSE,
      );
      // Build the table rows and columns.
      // The first nested level in the render array forms the table row, on which you
      // likely want to set #attributes and #weight.
      // Each child element on the second level represents a table column cell in the
      // respective table row, which are render elements on their own. For single
      // output elements, use the table cell itself for the render element. If a cell
      // should contain multiple elements, simply use nested sub-keys to build the
      // render element structure for drupal_render() as you would everywhere else.
      foreach ($texts as $id => $text) {
        if ($text['subcategory'] == $subcategory) {
          // Some table columns containing raw markup.
          $build[$subcategory_id]['list'][$id]['title'] = array(
            '#type' => 'item',
            '#title' => $text['title'],
            '#description' => $text['description'],
            '#description_display' => 'after',
            '#wrapper_attributes' => ['style' => 'width: 50%']
          );
          switch ($text['type']) {
            case 'text_format':
              $build[$subcategory_id]['list'][$id]['text'] = array(
                '#plain_text' => html_entity_decode(strip_tags($text['text'])),
              );
              break;
            case 'url':
              $url = customeredit_textToUrl($text['text']);
              if ($url !== FALSE) {
                $build[$subcategory_id]['list'][$id]['text'] = array(
                  '#type' => 'link',
                  '#title' => $text['text'],
                  '#url' => $url,
                );
              }
              else {
                $build[$subcategory_id]['list'][$id]['text'] = array(
                  '#markup' => '<i>' . $this->t('Invalid input') . '</i>',
                );
              }
              break;
            case 'button':
            case 'link':
              $values = json_decode($text['text'], TRUE);
              $url = customeredit_textToUrl(isset($values['url']) ? $values['url'] : '');
              $title = isset($values['title']) ? $values['title'] : '';
              if ($url !== FALSE && !empty($title)) {
                $build[$subcategory_id]['list'][$id]['text'] = array(
                  '#type' => 'link',
                  '#title' => $title,
                  '#url' => $url,
                );
                if ($text['type'] == 'button') {
                  $build[$subcategory_id]['list'][$id]['text']['#attributes'] = ['class' => ['button']];
                }
              }
              else {
                $build[$subcategory_id]['list'][$id]['text'] = array(
                  '#plain_text' => $title . " => " . isset($values['url']) ? $values['url'] : '',
                );
              }
              break;
            default:
              $build[$subcategory_id]['list'][$id]['text'] = array(
                '#plain_text' => $text['text'],
              );
          }
          $build[$subcategory_id]['list'][$id]['text']['#wrapper_attributes'] = ['style' => 'width: 50%'];

          // Operations (dropbutton) column.
          $build[$subcategory_id]['list'][$id]['operations'] = array(
            '#type' => 'operations',
            '#links' => array(),
            '#wrapper_attributes' => ['style' => 'width: 200px']
          );
          $build[$subcategory_id]['list'][$id]['operations']['#links']['edit'] = array(
            'title' => t('Edit'),
            'url' => Url::fromRoute('customertexts.edit', array('machine_name' => $text['machine_name'])),
          );
          if (!empty($text['location_url']) && !empty($text['location_selector'])) {
            $url = customeredit_textToUrl($text['location_url']);
            $url->setOption('query', array('ct_highlight' => $text['location_selector']));
            $build[$subcategory_id]['list'][$id]['operations']['#links']['show_location'] = array(
              'title' => t('Show location'),
              'url' => $url,
              'attributes' => array('target' => '_blank'),
            );
          }
          /*$build[$subcategory_id]['list'][$id]['operations']['#links']['delete'] = array(
            'title' => t('Delete'),
            'url' => Url::fromRoute('customertexts.delete', array('machine_name' => $text['machine_name'])),
          );*/
        }
      }
    }

    return $build;
  }

}