You are here

class WebformPluginElementController in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Controller/WebformPluginElementController.php \Drupal\webform\Controller\WebformPluginElementController

Controller for all webform elements.

Hierarchy

Expanded class hierarchy of WebformPluginElementController

File

src/Controller/WebformPluginElementController.php, line 18

Namespace

Drupal\webform\Controller
View source
class WebformPluginElementController 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;

  /**
   * The webform element plugin manager.
   *
   * @var \Drupal\webform\Plugin\WebformElementManagerInterface
   */
  protected $elementManager;

  /**
   * Constructs a WebformPluginElementController 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\webform\Plugin\WebformElementManagerInterface $element_manager
   *   A webform element plugin manager.
   */
  public function __construct(ModuleHandlerInterface $module_handler, ElementInfoManagerInterface $element_info, WebformElementManagerInterface $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.webform.element'));
  }

  /**
   * {@inheritdoc}
   */
  public function index() {
    $webform_form_element_rows = [];
    $element_rows = [];
    $excluded_elements = $this
      ->config('webform.settings')
      ->get('element.excluded_elements');
    $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 Webform Devel and UI module are
    // enabled.
    $test_element_enabled = $this->moduleHandler
      ->moduleExists('webform_devel') && $this->moduleHandler
      ->moduleExists('webform_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\webform\Plugin\WebformElementInterface $webform_element */
        $webform_element = $this->elementManager
          ->createInstance($element_plugin_id);
        $webform_element_plugin_definition = $this->elementManager
          ->getDefinition($element_plugin_id);
        $webform_element_info = $webform_element
          ->getInfo();

        // Title.
        if ($test_element_enabled) {
          $title = [
            'data' => [
              '#type' => 'link',
              '#title' => $element_plugin_id,
              '#url' => new Url('webform.reports_plugins.elements.test', [
                'type' => $element_plugin_id,
              ]),
              '#attributes' => [
                'class' => [
                  'webform-form-filter-text-source',
                ],
              ],
            ],
          ];
        }
        else {
          $title = new FormattableMarkup('<div class="webform-form-filter-text-source">@id</div>', [
            '@id' => $element_plugin_id,
          ]);
        }

        // Description.
        $description = [
          'data' => [
            'title_description' => [
              '#markup' => new FormattableMarkup('<strong>@label</strong><br />@description', [
                '@label' => $webform_element
                  ->getPluginLabel(),
                '@description' => $webform_element
                  ->getPluginDescription(),
              ]),
            ],
          ],
        ];

        // Add deprecated warning.
        if (!empty($webform_element_plugin_definition['deprecated'])) {
          $description['data']['deprecated'] = [
            '#type' => 'webform_message',
            '#message_message' => $webform_element_plugin_definition['deprecated_message'],
            '#message_type' => 'warning',
          ];
        }

        // Parent classes.
        $parent_classes = WebformReflectionHelper::getParentClasses($webform_element, 'WebformElementBase');

        // Formats.
        $default_format = $webform_element
          ->getItemDefaultFormat();
        $format_names = array_keys($webform_element
          ->getItemFormats());
        $formats = array_combine($format_names, $format_names);
        if (isset($formats[$default_format])) {
          $formats[$default_format] = '<b>' . $formats[$default_format] . '</b>';
        }

        // Related types.
        $related_types = $webform_element
          ->getRelatedTypes($element);

        // Dependencies.
        $dependencies = $webform_element_plugin_definition['dependencies'];

        // Webform element info.
        $webform_info_definitions = [
          'excluded' => isset($excluded_elements[$element_plugin_id]),
          'input' => $webform_element
            ->isInput($element),
          'container' => $webform_element
            ->isContainer($element),
          'root' => $webform_element
            ->isRoot(),
          'hidden' => $webform_element
            ->isHidden(),
          'composite' => $webform_element
            ->isComposite(),
          'multiple' => $webform_element
            ->supportsMultipleValues(),
          'multiline' => $webform_element
            ->isMultiline($element),
          'default_key' => $webform_element_plugin_definition['default_key'],
          'states_wrapper' => $webform_element_plugin_definition['states_wrapper'],
          'deprecated' => $webform_element_plugin_definition['deprecated'],
        ];
        $webform_info = [];
        foreach ($webform_info_definitions as $key => $value) {
          $webform_info[] = '<b>' . $key . '</b>: ' . ($value ? $this
            ->t('Yes') : $this
            ->t('No'));
        }

        // Element info.
        $element_info_definitions = [
          'input' => empty($webform_element_info['#input']) ? $this
            ->t('No') : $this
            ->t('Yes'),
          'theme' => isset($webform_element_info['#theme']) ? $webform_element_info['#theme'] : 'N/A',
          'theme_wrappers' => isset($webform_element_info['#theme_wrappers']) ? implode('; ', $webform_element_info['#theme_wrappers']) : 'N/A',
        ];
        $element_info = [];
        foreach ($element_info_definitions as $key => $value) {
          $element_info[] = '<b>' . $key . '</b>: ' . $value;
        }

        // Properties.
        $properties = [];
        $element_default_properties = $webform_element
          ->getDefaultProperties();
        foreach ($element_default_properties as $key => $default_value) {
          if (is_bool($default_value)) {
            $data_type = 'boolean';
          }
          elseif (is_array($default_value)) {
            $data_type = 'array';
          }
          elseif (is_numeric($default_value) || is_null($default_value)) {
            $data_type = 'number';
          }
          else {
            $data_type = 'string';
          }
          $default_value = $default_value ? ' ⇒ ' . json_encode($default_value) : '';
          if (!isset($default_properties[$key])) {
            $properties[$key] = '<b>#' . $key . '</b> [' . $data_type . ']' . $default_value;
            unset($element_default_properties[$key]);
          }
          else {
            $element_default_properties[$key] = '#' . $key . ' [' . $data_type . ']' . $default_value;
          }
        }
        $properties += $element_default_properties;

        // Operations.
        $operations = [];
        if ($test_element_enabled) {
          $operations['test'] = [
            'title' => $this
              ->t('Test'),
            'url' => new Url('webform.reports_plugins.elements.test', [
              'type' => $element_plugin_id,
            ]),
          ];
        }
        if ($api_url = $webform_element
          ->getPluginApiUrl()) {
          $operations['documentation'] = [
            'title' => $this
              ->t('API Docs'),
            'url' => $api_url,
          ];
        }
        $webform_form_element_rows[$element_plugin_id] = [
          'data' => [
            $title,
            $description,
            [
              'data' => [
                '#markup' => implode('<br /> → ', $parent_classes),
              ],
              'nowrap' => 'nowrap',
            ],
            [
              'data' => [
                '#markup' => implode('<br />', $webform_info),
              ],
              'nowrap' => 'nowrap',
            ],
            [
              'data' => [
                '#markup' => implode('<br />', $element_info),
              ],
              'nowrap' => 'nowrap',
            ],
            [
              'data' => [
                '#markup' => implode('<br />' . PHP_EOL, $properties),
              ],
              'nowrap' => 'nowrap',
            ],
            $formats ? [
              'data' => [
                '#markup' => '• ' . implode('<br />• ', $formats),
              ],
              'nowrap' => 'nowrap',
            ] : '',
            $related_types ? [
              'data' => [
                '#markup' => '• ' . implode('<br />• ', $related_types),
              ],
              'nowrap' => 'nowrap',
            ] : '<' . $this
              ->t('none') . '>',
            $dependencies ? [
              'data' => [
                '#markup' => '• ' . implode('<br />• ', $dependencies),
              ],
              'nowrap' => 'nowrap',
            ] : '',
            $element_plugin_definition['provider'],
            $webform_element_plugin_definition['provider'],
            $operations ? [
              'data' => [
                '#type' => 'operations',
                '#links' => $operations,
                '#prefix' => '<div class="webform-dropbutton">',
                '#suffix' => '</div>',
              ],
            ] : '',
          ],
        ];
        if (isset($excluded_elements[$element_plugin_id])) {
          $webform_form_element_rows[$element_plugin_id]['class'] = [
            'color-warning',
          ];
        }
      }
      else {
        $element_rows[$element_plugin_id] = [
          $element_plugin_id,
          $element_plugin_definition['provider'],
        ];
      }
    }
    $build = [];

    // Filter.
    $build['filter'] = [
      '#type' => 'search',
      '#title' => $this
        ->t('Filter'),
      '#title_display' => 'invisible',
      '#size' => 30,
      '#placeholder' => $this
        ->t('Filter by element name'),
      '#attributes' => [
        'class' => [
          'webform-form-filter-text',
        ],
        'data-element' => '.webform-element-plugin-table',
        'data-summary' => '.webform-element-plugin-summary',
        'data-item-singlular' => $this
          ->t('element'),
        'data-item-plural' => $this
          ->t('elements'),
        'title' => $this
          ->t('Enter a part of the element type to filter by.'),
        'autofocus' => 'autofocus',
      ],
    ];

    // Settings.
    $build['settings'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Edit configuration'),
      '#url' => Url::fromRoute('webform.config.elements'),
      '#attributes' => [
        'class' => [
          'button',
          'button--small',
        ],
        'style' => 'float: right',
      ],
    ];

    // Display info.
    $build['info'] = [
      '#markup' => $this
        ->t('@total elements', [
        '@total' => count($webform_form_element_rows),
      ]),
      '#prefix' => '<p class="webform-element-plugin-summary">',
      '#suffix' => '</p>',
    ];
    ksort($webform_form_element_rows);
    $build['webform_elements'] = [
      '#type' => 'table',
      '#header' => [
        $this
          ->t('Name'),
        $this
          ->t('Label/Description'),
        $this
          ->t('Class hierarchy'),
        $this
          ->t('Webform info'),
        $this
          ->t('Element info'),
        $this
          ->t('Properties'),
        $this
          ->t('Formats'),
        $this
          ->t('Related'),
        $this
          ->t('Dependencies'),
        $this
          ->t('Provided by'),
        $this
          ->t('Integrated by'),
        $this
          ->t('Operations'),
      ],
      '#rows' => $webform_form_element_rows,
      '#sticky' => TRUE,
      '#attributes' => [
        'class' => [
          'webform-element-plugin-table',
        ],
      ],
    ];
    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 Webform 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'][] = 'webform/webform.admin';
    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.
WebformPluginElementController::$elementInfo protected property A element info manager.
WebformPluginElementController::$elementManager protected property The webform element plugin manager.
WebformPluginElementController::$moduleHandler protected property The module handler. Overrides ControllerBase::$moduleHandler
WebformPluginElementController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
WebformPluginElementController::getClassName protected function Get a class's name without its namespace.
WebformPluginElementController::index public function
WebformPluginElementController::__construct public function Constructs a WebformPluginElementController object.