You are here

class YamlFormPluginElementController in YAML Form 8

Controller for all form elements.

Hierarchy

Expanded class hierarchy of YamlFormPluginElementController

File

src/Controller/YamlFormPluginElementController.php, line 18

Namespace

Drupal\yamlform\Controller
View source
class YamlFormPluginElementController extends ControllerBase implements ContainerInjectionInterface {

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * A element info manager.
   *
   * @var \Drupal\Core\Render\ElementInfoManagerInterface
   */
  protected $elementInfo;

  /**
   * A form element plugin manager.
   *
   * @var \Drupal\yamlform\YamlFormElementManagerInterface
   */
  protected $elementManager;

  /**
   * Constructs a YamlFormPluginBaseController object.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Render\ElementInfoManagerInterface $element_info
   *   A element info plugin manager.
   * @param \Drupal\yamlform\YamlFormElementManagerInterface $element_manager
   *   A form element plugin manager.
   */
  public function __construct(ModuleHandlerInterface $module_handler, ElementInfoManagerInterface $element_info, YamlFormElementManagerInterface $element_manager) {
    $this->moduleHandler = $module_handler;
    $this->elementInfo = $element_info;
    $this->elementManager = $element_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('module_handler'), $container
      ->get('plugin.manager.element_info'), $container
      ->get('plugin.manager.yamlform.element'));
  }

  /**
   * {@inheritdoc}
   */
  public function index() {
    $yamlform_form_element_rows = [];
    $element_rows = [];
    $default_properties = [
      // Element settings.
      'title',
      'description',
      'default_value',
      // Form display.
      'title_display',
      'description_display',
      'field_prefix',
      'field_suffix',
      // Form validation.
      'required',
      'required_error',
      'unique',
      // Submission display.
      'format',
      // Attributes.
      'wrapper_attributes',
      'attributes',
      // Administration.
      'admin_title',
      'private',
      // Flexbox.
      'flex',
      // Conditional logic.
      'states',
      // Element access.
      'access_create_roles',
      'access_create_users',
      'access_update_roles',
      'access_update_users',
      'access_view_roles',
      'access_view_users',
    ];
    $default_properties = array_combine($default_properties, $default_properties);

    // Test element is only enabled if the YAML Form Devel and UI module are
    // enabled.
    $test_element_enabled = $this->moduleHandler
      ->moduleExists('yamlform_devel') && $this->moduleHandler
      ->moduleExists('yamlform_ui') ? TRUE : FALSE;

    // Define a default element used to get default properties.
    $element = [
      '#type' => 'element',
    ];
    $element_plugin_definitions = $this->elementInfo
      ->getDefinitions();
    foreach ($element_plugin_definitions as $element_plugin_id => $element_plugin_definition) {
      if ($this->elementManager
        ->hasDefinition($element_plugin_id)) {

        /** @var \Drupal\yamlform\YamlFormElementInterface $yamlform_element */
        $yamlform_element = $this->elementManager
          ->createInstance($element_plugin_id);
        $yamlform_element_plugin_definition = $this->elementManager
          ->getDefinition($element_plugin_id);
        $yamlform_element_info = $yamlform_element
          ->getInfo();
        $parent_classes = YamlFormReflectionHelper::getParentClasses($yamlform_element, 'YamlFormElementBase');
        $default_format = $yamlform_element
          ->getDefaultFormat();
        $format_names = array_keys($yamlform_element
          ->getFormats());
        $formats = array_combine($format_names, $format_names);
        if (isset($formats[$default_format])) {
          $formats[$default_format] = '<b>' . $formats[$default_format] . '</b>';
        }
        $related_types = $yamlform_element
          ->getRelatedTypes($element);
        $yamlform_info_definitions = [
          'input' => $yamlform_element
            ->isInput($element),
          'container' => $yamlform_element
            ->isContainer($element),
          'root' => $yamlform_element
            ->isRoot(),
          'hidden' => $yamlform_element
            ->isHidden(),
          'multiline' => $yamlform_element
            ->isMultiline($element),
          'multiple' => $yamlform_element
            ->hasMultipleValues($element),
          'states_wrapper' => $yamlform_element_plugin_definition['states_wrapper'],
        ];
        $yamlform_info = [];
        foreach ($yamlform_info_definitions as $key => $value) {
          $yamlform_info[] = '<b>' . $key . '</b>: ' . ($value ? $this
            ->t('Yes') : $this
            ->t('No'));
        }
        $element_info_definitions = [
          'input' => empty($yamlform_element_info['#input']) ? $this
            ->t('No') : $this
            ->t('Yes'),
          'theme' => isset($yamlform_element_info['#theme']) ? $yamlform_element_info['#theme'] : 'N/A',
          'theme_wrappers' => isset($yamlform_element_info['#theme_wrappers']) ? implode('; ', $yamlform_element_info['#theme_wrappers']) : 'N/A',
        ];
        $element_info = [];
        foreach ($element_info_definitions as $key => $value) {
          $element_info[] = '<b>' . $key . '</b>: ' . $value;
        }
        $properties = [];
        $element_default_properties = array_keys($yamlform_element
          ->getDefaultProperties());
        foreach ($element_default_properties as $key => $value) {
          if (!isset($default_properties[$value])) {
            $properties[$key] = '<b>#' . $value . '</b>';
            unset($element_default_properties[$key]);
          }
          else {
            $element_default_properties[$key] = '#' . $value;
          }
        }
        $properties += $element_default_properties;
        if (count($properties) >= 20) {
          $properties = array_slice($properties, 0, 20) + [
            '...' => '...',
          ];
        }
        $operations = [];
        if ($test_element_enabled) {
          $operations['test'] = [
            'title' => $this
              ->t('Test'),
            'url' => new Url('yamlform.element_plugins.test', [
              'type' => $element_plugin_id,
            ]),
          ];
        }
        if ($api_url = $yamlform_element
          ->getPluginApiUrl()) {
          $operations['documentation'] = [
            'title' => $this
              ->t('API Docs'),
            'url' => $api_url,
          ];
        }
        $yamlform_form_element_rows[$element_plugin_id] = [
          'data' => [
            new FormattableMarkup('<div class="yamlform-form-filter-text-source">@id</div>', [
              '@id' => $element_plugin_id,
            ]),
            $yamlform_element
              ->getPluginLabel(),
            [
              'data' => [
                '#markup' => implode('<br/> → ', $parent_classes),
              ],
              'nowrap' => 'nowrap',
            ],
            [
              'data' => [
                '#markup' => implode('<br/>', $yamlform_info),
              ],
              'nowrap' => 'nowrap',
            ],
            [
              'data' => [
                '#markup' => implode('<br/>', $element_info),
              ],
              'nowrap' => 'nowrap',
            ],
            [
              'data' => [
                '#markup' => implode('<br/>', $properties),
              ],
            ],
            $formats ? [
              'data' => [
                '#markup' => '• ' . implode('<br/>• ', $formats),
              ],
              'nowrap' => 'nowrap',
            ] : '',
            $related_types ? [
              'data' => [
                '#markup' => '• ' . implode('<br/>• ', $related_types),
              ],
              'nowrap' => 'nowrap',
            ] : '<' . $this
              ->t('none') . '>',
            $element_plugin_definition['provider'],
            $yamlform_element_plugin_definition['provider'],
            $operations ? [
              'data' => [
                '#type' => 'operations',
                '#links' => $operations,
              ],
            ] : '',
          ],
        ];
      }
      else {
        $element_rows[$element_plugin_id] = [
          $element_plugin_id,
          $element_plugin_definition['provider'],
        ];
      }
    }
    $build = [];
    $build['filter'] = [
      '#type' => 'search',
      '#title' => $this
        ->t('Filter'),
      '#title_display' => 'invisible',
      '#size' => 30,
      '#placeholder' => $this
        ->t('Filter by element name'),
      '#attributes' => [
        'class' => [
          'yamlform-form-filter-text',
        ],
        'data-element' => '.yamlform-element-plugin',
        'title' => $this
          ->t('Enter a part of the element type to filter by.'),
        'autofocus' => 'autofocus',
      ],
    ];
    ksort($yamlform_form_element_rows);
    $build['yamlform_elements'] = [
      '#type' => 'table',
      '#header' => [
        $this
          ->t('Name'),
        $this
          ->t('Label'),
        $this
          ->t('Class hierarchy'),
        $this
          ->t('YAML Form info'),
        $this
          ->t('Element info'),
        $this
          ->t('Properties'),
        $this
          ->t('Formats'),
        $this
          ->t('Related'),
        $this
          ->t('Provided by'),
        $this
          ->t('Integrated by'),
        $this
          ->t('Operations'),
      ],
      '#rows' => $yamlform_form_element_rows,
      '#attributes' => [
        'class' => [
          'yamlform-element-plugin',
        ],
      ],
    ];
    ksort($element_rows);
    $build['elements'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Additional elements'),
      '#description' => $this
        ->t('Below are elements that are available but do not have a YAML Form Element integration plugin.'),
      'table' => [
        '#type' => 'table',
        '#header' => [
          $this
            ->t('Name'),
          $this
            ->t('Provided by'),
        ],
        '#rows' => $element_rows,
        '#sticky' => TRUE,
      ],
    ];
    $all_translatable_properties = $this->elementManager
      ->getTranslatableProperties();
    $all_properties = $this->elementManager
      ->getAllProperties();
    foreach ($all_translatable_properties as $key => $value) {
      $all_translatable_properties[$key] = [
        '#markup' => $value,
        '#prefix' => '<strong>',
        '#suffix' => '</strong>',
        '#weight' => -10,
      ];
    }
    foreach ($all_properties as $key => $value) {

      // Remove all composite properties.
      if (strpos($key, '__')) {
        unset($all_properties[$key]);
      }
    }
    $build['properties'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Element properties'),
      '#description' => $this
        ->t('Below are all available element properties with translatable properties in <strong>bold</strong>.'),
      'list' => [
        '#theme' => 'item_list',
        '#items' => $all_translatable_properties + $all_properties,
      ],
    ];
    $build['#attached']['library'][] = 'yamlform/yamlform.admin';
    $build['#attached']['library'][] = 'yamlform/yamlform.form';
    return $build;
  }

  /**
   * Get a class's name without its namespace.
   *
   * @param string $class
   *   A class.
   *
   * @return string
   *   The class's name without its namespace.
   */
  protected function getClassName($class) {
    $parts = preg_split('#\\\\#', $class);
    return end($parts);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.
YamlFormPluginElementController::$elementInfo protected property A element info manager.
YamlFormPluginElementController::$elementManager protected property A form element plugin manager.
YamlFormPluginElementController::$moduleHandler protected property The module handler. Overrides ControllerBase::$moduleHandler
YamlFormPluginElementController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
YamlFormPluginElementController::getClassName protected function Get a class's name without its namespace.
YamlFormPluginElementController::index public function
YamlFormPluginElementController::__construct public function Constructs a YamlFormPluginBaseController object.