You are here

wingsuit_lb.module in Gin Layout Builder 8.2

File

modules/wingsuit_lb/wingsuit_lb.module
View source
<?php

use Drupal\Core\Form\FormStateInterface;
use Drupal\ui_patterns\Definition\PatternDefinition;
use Drupal\node\NodeInterface;
use Drupal\Core\Url;

/**
 * Implements hook_page_attachments().
 */
function wingsuit_lb_page_attachments(array &$attachments) {
  if (gin_lb_is_layout_builder_route() && gin_lb_is_valid_theme()) {
    $attachments['#attached']['library'][] = 'wingsuit_lb/core';
  }
}

/**
 * Implements hook_ui_patterns_layouts_display_settings_form_alter().
 */
function wingsuit_lb_ui_patterns_layouts_display_settings_form_alter(&$form, PatternDefinition $definition, $configuration) {
  $form['#type'] = 'container';
  $form['field_templates']['#type'] = 'hidden';
  $form['settings']['#type'] = 'container';
}

/**
 * Implements hook_form_alter().
 */
function wingsuit_lb_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $node = \Drupal::routeMatch()
    ->getParameter('node');
  if ($node instanceof NodeInterface && $form_id === 'node_' . $node
    ->bundle() . '_layout_builder_form') {
    $add_template_to_library_url = Url::fromRoute('section_library.add_template_to_library', [
      'section_storage_type' => 'overrides',
      'section_storage' => $node
        ->getEntityTypeId() . '.' . $node
        ->id(),
      'delta' => 1,
    ], [
      'attributes' => [
        'class' => [
          'glb-button ws-lb-button',
          'use-ajax',
        ],
        'data-dialog-type' => 'dialog',
        'data-dialog-renderer' => 'off_canvas',
      ],
    ]);
    $form['actions']['add_to_library'] = [
      '#type' => 'link',
      '#weight' => 10,
      '#url' => $add_template_to_library_url,
      '#title' => [
        '#markup' => '<div class="ws-lb-button__icon"></div>' . t('Add to library'),
      ],
      '#access' => $add_template_to_library_url
        ->access(),
    ];
    $form['actions']['submit']['#value'] = t('Save and Preview');
    $form['actions']['layout_builder_save_and_edit_layout']['#value'] = t('Save');
  }
  if (in_array($form_id, [
    'section_library_add_template_to_library',
    'section_library_add_section_to_library',
  ])) {
    $form['#after_build'][] = 'gin_lb_after_build';
    $form['#gin_lb_form'] = TRUE;
    $form['#attributes']['class'][] = 'glb-form';
  }
}

/**
 * Implements hook_preprocess_wingsuit_lb_icon().
 */
function wingsuit_lb_preprocess_wingsuit_lb_icon(&$variables) {
  $uri = $variables['uri'];
  $extension = pathinfo($uri, PATHINFO_EXTENSION);
  if ($extension === 'svg') {
    if (substr($uri, 0, 1) === '/') {

      #
      $uri = substr($uri, 1);
    }
    if (file_exists($uri)) {
      $variables['svg'] = $uri;
    }
  }
}

/**
 * Implements hook_theme().
 */
function wingsuit_lb_theme($existing, $type, $theme, $path) {
  return [
    'wingsuit_lb_icon' => [
      'variables' => [
        'uri' => NULL,
        'width' => NULL,
        'height' => NULL,
        'svg' => NULL,
        'alt' => NULL,
      ],
    ],
  ];
}