You are here

paragraph_view_mode.module in Paragraph View Mode 8

Same filename and directory in other branches
  1. 2.x paragraph_view_mode.module

Contains paragraphs_view_mode.module functions.

File

paragraph_view_mode.module
View source
<?php

/**
 * @file
 * Contains paragraphs_view_mode.module functions.
 */
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\paragraph_view_mode\StorageManagerInterface;
use Drupal\paragraph_view_mode\ViewModeInterface;

/**
 * Implements hook_help().
 */
function paragraph_view_mode_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.paragraph_view_mode':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('This module allows to dynamically pick the display mode of the paragraph during adding/editing the paragraph item, by creating a field with available view modes.') . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Enable view mode field on paragraph type') . '</dt>';
      $output .= '<dd>' . t('Go to edit page of your paragraph type and check option "Enable paragraph view mode field on this paragraph type.". Save.') . '</dd>';
      $output .= '<dt>' . t('Add field to add/edit form') . '</dt>';
      $output .= '<dd>' . t('Go to the "Manage form display" tab and place your field anywhere you want (except the disabled section).') . '</dd>';
      $output .= '<dt>' . t('Widget settings') . '</dt>';
      $output .= '<dd>' . t('You can configure the field widget, by selecting which view modes you want to allow to view during adding/editing the content. If you do not select any view mode, the "default" will be displayed.') . '</dd>';
      $output .= '</dl>';
      return $output;
  }
}

/**
 * Implements hook_entity_view_mode_alter().
 */
function paragraph_view_mode_entity_view_mode_alter(&$view_mode, EntityInterface $entity, $context) {
  if (!$entity instanceof ParagraphInterface) {
    return;
  }
  $field_name = StorageManagerInterface::FIELD_NAME;
  if (!$entity
    ->hasField($field_name) || $view_mode === ViewModeInterface::PREVIEW) {
    return;
  }
  $view_mode = $entity
    ->get($field_name)->value ?: $view_mode;
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function paragraph_view_mode_form_paragraphs_type_edit_form_alter(&$form, FormStateInterface $form_state) {
  $paragraph = $form_state
    ->getFormObject();
  $paragraphType = $paragraph
    ->getEntity();
  $config_name = StorageManagerInterface::CONFIG_NAME;
  $form[$config_name] = [
    '#title' => t(StorageManagerInterface::FIELD_LABEL),
    '#type' => 'details',
    '#tree' => TRUE,
    '#open' => TRUE,
  ];
  $form[$config_name]['enabled'] = [
    '#title' => t('Enable @label field on this @type type.', [
      '@label' => StorageManagerInterface::FIELD_LABEL,
      '@type' => StorageManagerInterface::ENTITY_TYPE,
    ]),
    '#type' => 'checkbox',
    '#default_value' => $paragraphType
      ->getThirdPartySetting($config_name, 'enabled') ?: FALSE,
  ];
  $form[$config_name]['label'] = [
    '#title' => t('Label'),
    "#description" => t('If you do not set the label, the default will be used instead: %label', [
      '%label' => StorageManagerInterface::FIELD_LABEL,
    ]),
    '#type' => 'textfield',
    '#default_value' => $paragraphType
      ->getThirdPartySetting($config_name, 'label') ?: NULL,
    '#states' => [
      'visible' => [
        ':input[name="paragraph_view_mode[enabled]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['actions']['submit']['#submit'][] = 'paragraph_view_mode_form_paragraphs_type_edit_form_submit';
}

/**
 * Paragraph view mode form submit.
 *
 * @param array $form
 *   Form structure.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   Form state object.
 */
function paragraph_view_mode_form_paragraphs_type_edit_form_submit(array &$form, FormStateInterface $form_state) {
  $config_name = StorageManagerInterface::CONFIG_NAME;
  $enable = $form_state
    ->getValue([
    $config_name,
    'enabled',
  ]);
  $label = $form_state
    ->getValue([
    $config_name,
    'label',
  ], NULL);
  $paragraphType = $form_state
    ->getFormObject()
    ->getEntity();
  $bundle = $paragraphType
    ->getOriginalId();

  /** @var \Drupal\paragraph_view_mode\StorageManagerInterface $storage_manager */
  $storage_manager = \Drupal::service('paragraph_view_mode.storage_manager');
  if (!empty($enable)) {
    $enable = $storage_manager
      ->addField($bundle);
    if ($enable) {
      $storage_manager
        ->addToFormDisplay($bundle);
    }
    if ($label) {
      $storage_manager
        ->setFieldLabel($bundle, $label);
      $paragraphType
        ->setThirdPartySetting($config_name, 'label', $label);
    }
  }
  else {
    $enable = !$storage_manager
      ->deleteField($bundle);
  }
  $paragraphType
    ->setThirdPartySetting($config_name, 'enabled', $enable);
  $paragraphType
    ->save();
}