Commit e79053c3 authored by Volodymyr Mostepaniuk's avatar Volodymyr Mostepaniuk
Browse files

Issue #3295753 by mostepaniukvm: Initial forms implementation. Basic config schema.

parent a15968a6
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
core.entity_view_display.*.*.*.third_party.ce:
  type: mapping
  label: 'Per-view-mode Custom elements settings'
  mapping:
    enabled:
      type: boolean
      label: 'Whether the Custom Elements Display builder is enabled for this display'
    custom_element_fields:
      type: sequence
      sequence:
        type: custom_elements_ui.field

custom_elements_ui.field:
  type: mapping
  label: 'CE field'
  mapping:
    field_name:
      type: string
      label: 'Field Name'
    label:
      type: string
      label: 'CE name'
    is_slot:
      type: bool
      label: 'is Slot'
    ce_formatter:
      type: string
      label: 'CE Formatter'
+7 −8
Original line number Diff line number Diff line
@@ -8,8 +8,9 @@
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\custom_elements_ui\Form\DefaultsEntityForm;
use Drupal\custom_elements_ui\Form\OverridesEntityForm;
use Drupal\field_ui\FieldUI;
use Drupal\field_ui\Form\EntityViewDisplayEditForm;

/**
 * Implements hook_entity_type_alter().
@@ -17,12 +18,12 @@ use Drupal\field_ui\Form\EntityViewDisplayEditForm;
function custom_elements_ui_entity_type_alter(array &$entity_types) {
  /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
  $entity_types['entity_view_display']
    ->setFormClass('custom_elements_builder', EntityViewDisplayEditForm::class);
    ->setFormClass('custom_elements_builder', DefaultsEntityForm::class);

  // Ensure every fieldable entity type has a layout form.
  // Ensure every fieldable entity type has a custom_elements form.
  foreach ($entity_types as $entity_type) {
    if ($entity_type->entityClassImplements(FieldableEntityInterface::class)) {
      $entity_type->setFormClass('custom_elements_builder', EntityViewDisplayEditForm::class);
      $entity_type->setFormClass('custom_elements_builder', OverridesEntityForm::class);
    }
  }
}
@@ -31,14 +32,14 @@ function custom_elements_ui_entity_type_alter(array &$entity_types) {
* Implements hook_form_FORM_ID_alter() for \Drupal\field_ui\Form\EntityFormDisplayEditForm.
*/
function custom_elements_ui_form_entity_view_display_edit_form_alter(&$form, FormStateInterface $form_state) {
  /** @var \Drupal\layout_builder\Form\LayoutBuilderEntityViewDisplayForm $display_form */
  /** @var \Drupal\Core\Entity\EntityFormInterface $display_form */
  $display_form = $form_state->getFormObject();
  /** @var \Drupal\Core\Entity\Entity\EntityViewDisplay $display_entity */
  $display_entity = $display_form->getEntity();
  $is_enabled = $display_entity->getThirdPartySetting('ce', 'enabled');

  // Hide the table of fields same as in field_ui.
  if ($is_enabled) {
    // Hide the table of fields.
    $form['fields']['#access'] = FALSE;
    $form['#fields'] = [];
    $form['#extra'] = [];
@@ -52,7 +53,6 @@ function custom_elements_ui_form_entity_view_display_edit_form_alter(&$form, For
  $route_name = "entity.entity_ce_display.{$display_entity->getTargetEntityTypeId()}.{$mode_component}";
  $ce_builder_url = Url::fromRoute($route_name, $route_parameters);


  $form['manage_custom_elements'] = [
    '#type' => 'link',
    '#title' => t('Manage custom elements'),
@@ -72,7 +72,6 @@ function custom_elements_ui_form_entity_view_display_edit_form_alter(&$form, For
  $form['ce']['enabled'] = [
    '#type' => 'checkbox',
    '#title' => t('Use Custom Elements Builder'),
    // @todo What about "Custom Elements Builder" naming?
    '#default_value' => $is_enabled,
  ];

+68 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\custom_elements_ui\Form;

use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\layout_builder\SectionStorageInterface;

/**
 * Provides a form containing the Layout Builder UI for defaults.
 *
 * @internal
 *   Form classes are internal.
 */
class DefaultsEntityForm extends EntityForm {

  /**
   * {@inheritdoc}
   */
  public function getBaseFormId() {
    return $this->getEntity()->getEntityTypeId() . '_ce_builder_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL) {
    $form['ce'] = [
      '#type' => 'markup',
      '#markup' => 'Coming soon...',
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function buildEntity(array $form, FormStateInterface $form_state) {
    $entity = parent::buildEntity($form, $form_state);
    return $entity;
  }

  /**
   * {@inheritdoc}
   */
  public function getEntityFromRouteMatch(RouteMatchInterface $route_match, $entity_type_id) {
    $route_parameters = $route_match->getParameters()->all();

    return $this->entityTypeManager->getStorage('entity_view_display')->load($route_parameters['entity_type_id'] . '.' . $route_parameters['bundle'] . '.' . $route_parameters['view_mode_name']);
  }

  /**
   * {@inheritdoc}
   */
  protected function actions(array $form, FormStateInterface $form_state) {
    $actions = parent::actions($form, $form_state);
    return $actions;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    return parent::save($form, $form_state);
  }

}
+61 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\custom_elements_ui\Form;

use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\layout_builder\SectionStorageInterface;

/**
 * Provides a form containing the Layout Builder UI for overrides.
 *
 * @internal
 *   Form classes are internal.
 */
class OverridesEntityForm extends ContentEntityForm {

  /**
   * {@inheritdoc}
   */
  public function getBaseFormId() {
    return $this->getEntity()->getEntityTypeId() . '_ce_builder_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL) {
    $form['ce'] = [
      '#type' => 'markup',
      '#markup' => 'Coming soon...',
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function getEntityFromRouteMatch(RouteMatchInterface $route_match, $entity_type_id) {
    $route_parameters = $route_match->getParameters()->all();

    return $this->entityTypeManager->getStorage('entity_view_display')->load($route_parameters['entity_type_id'] . '.' . $route_parameters['bundle'] . '.' . $route_parameters['view_mode_name']);
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $return = parent::save($form, $form_state);
    return $return;
  }

  /**
   * {@inheritdoc}
   */
  protected function actions(array $form, FormStateInterface $form_state) {
    $actions = parent::actions($form, $form_state);
    return $actions;
  }

}
+13 −9
Original line number Diff line number Diff line
@@ -10,8 +10,6 @@ use Symfony\Component\Routing\RouteCollection;

/**
 * Subscriber for Custom Elements UI routes.
 *
 * @todo Ensure it's not enough to announce new routes in .routing.yml.
 */
class RouteSubscriber extends RouteSubscriberBase {

@@ -50,23 +48,29 @@ class RouteSubscriber extends RouteSubscriberBase {
            'type' => 'entity:' . $bundle_entity_type,
          ];
        }
        // @todo Needed?
        // Special parameter used to easily recognize all Custom Elements UI
        // Special parameter used to easily recognize all _field_ui routes.
        // routes.
        $options['_custom_elements_ui'] = TRUE;
        // Usage example: Drupal\Core\Entity\Enhancer\EntityBundleRouteEnhancer
        $options['_field_ui'] = TRUE;

        $defaults = [
          'entity_type_id' => $entity_type_id,
        ];
        // If the entity type has no bundles and it doesn't use {bundle} in its
        // admin path, use the entity type.
        // If the entity type has no bundles and it doesn't use {bundle} in its
        // admin path, use the entity type.
        if (strpos($path, '{bundle}') === FALSE) {
          $defaults['bundle'] = !$entity_type->hasKey('bundle') ? $entity_type_id : '';
          if (!$entity_type->hasKey('bundle')) {
            $defaults['bundle'] = $entity_type_id;
          }
          else {
            $defaults['bundle_key'] = $entity_type->getBundleEntityType();
          }
        }

        // @todo Use own route callback.
        $route = new Route(
          "$path/ce",
          "$path/display/ce",
          [
            '_entity_form' => 'entity_view_display.custom_elements_builder',
            '_title' => 'Manage CE',
@@ -79,7 +83,7 @@ class RouteSubscriber extends RouteSubscriberBase {
        $collection->add("entity.entity_ce_display.{$entity_type_id}.default", $route);

        $route = new Route(
          "$path/ce/{view_mode_name}",
          "$path/display/{view_mode_name}/ce",
          [
            '_entity_form' => 'entity_view_display.custom_elements_builder',
            '_title' => 'Manage CE',