You are here

class Pattern in UI Patterns 8

Same name in this branch
  1. 8 src/Element/Pattern.php \Drupal\ui_patterns\Element\Pattern
  2. 8 modules/ui_patterns_ds/src/Plugin/DsFieldTemplate/Pattern.php \Drupal\ui_patterns_ds\Plugin\DsFieldTemplate\Pattern
  3. 8 modules/ui_patterns_views/src/Plugin/views/row/Pattern.php \Drupal\ui_patterns_views\Plugin\views\row\Pattern

Plugin for the expert field template.

Plugin annotation


@DsFieldTemplate(
  id = "pattern",
  title = @Translation("Pattern"),
  theme = "pattern_ds_field_template",
)

Hierarchy

Expanded class hierarchy of Pattern

3 string references to 'Pattern'
PatternDisplayFormTrait::buildPatternDisplayForm in src/Form/PatternDisplayFormTrait.php
Build pattern display form.
UiPatternsFieldGroupSettingsTest::testWarningMessage in modules/ui_patterns_field_group/tests/src/FunctionalJavascript/UiPatternsFieldGroupSettingsTest.php
Make sure a warning message is displayed when using pattern formatter.
UiPatternsFieldSettingsTest::testUiPatternsFieldSettings in modules/ui_patterns_ds/tests/src/FunctionalJavascript/UiPatternsFieldSettingsTest.php
Tests field template settings.

File

modules/ui_patterns_ds/src/Plugin/DsFieldTemplate/Pattern.php, line 24

Namespace

Drupal\ui_patterns_ds\Plugin\DsFieldTemplate
View source
class Pattern extends DsFieldTemplateBase implements ContainerFactoryPluginInterface {
  use PatternDisplayFormTrait;

  /**
   * Module Handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler = NULL;

  /**
   * UI Patterns manager.
   *
   * @var \Drupal\ui_patterns\UiPatternsManager
   */
  protected $patternsManager;

  /**
   * UI Patterns manager.
   *
   * @var \Drupal\ui_patterns\UiPatternsSourceManager
   */
  protected $sourceManager;

  /**
   * Current $_POST parameters.
   *
   * @var \Symfony\Component\HttpFoundation\ParameterBag
   */
  protected $parameters;

  /**
   * Entity field manager service.
   *
   * @var \Drupal\Core\Entity\EntityFieldManager
   */
  protected $fieldManager;

  /**
   * Pattern constructor.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\ui_patterns\UiPatternsManager $patterns_manager
   *   UI Patterns manager.
   * @param \Drupal\ui_patterns\UiPatternsSourceManager $source_manager
   *   UI Patterns source manager.
   * @param \Symfony\Component\HttpFoundation\RequestStack $parameters
   *   Current $_POST parameters.
   * @param \Drupal\Core\Entity\EntityFieldManager $field_manager
   *   Field manager.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   Module handler.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, UiPatternsManager $patterns_manager, UiPatternsSourceManager $source_manager, RequestStack $parameters, EntityFieldManager $field_manager, ModuleHandlerInterface $module_handler) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->patternsManager = $patterns_manager;
    $this->sourceManager = $source_manager;
    $this->parameters = $parameters
      ->getCurrentRequest()->request;
    $this->fieldManager = $field_manager;
    $this->moduleHandler = $module_handler;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('plugin.manager.ui_patterns'), $container
      ->get('plugin.manager.ui_patterns_source'), $container
      ->get('request_stack'), $container
      ->get('entity_field.manager'), $container
      ->get('module_handler'));
  }

  /**
   * {@inheritdoc}
   */
  public function alterForm(&$form) {
    $context = $this
      ->getContext();
    if ($this
      ->isSupportedField($context)) {
      $this
        ->buildPatternDisplayForm($form, 'ds_field_template', $context, $this
        ->getConfiguration());
    }
    else {
      $form['#markup'] = $this
        ->t("The current field is not supported.");
    }
  }

  /**
   * Get source field plugin context.
   *
   * @return array
   *   Context array.
   */
  protected function getContext() {
    $fields = $this->parameters
      ->get('fields');
    $field_name = $this
      ->getCurrentField();
    return [
      'field_name' => $field_name,
      'field_settings' => $fields[$field_name],
      'entity_type' => $this->parameters
        ->get('ds_entity_type'),
      'bundle' => $this->parameters
        ->get('ds_bundle'),
      'view_mode' => $this->parameters
        ->get('ds_view_mode'),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'pattern' => '',
      'pattern_variant' => '',
      'pattern_mapping' => [],
    ];
  }

  /**
   * Get name of field currently being edited.
   *
   * @return string
   *   Name of field currently being edited.
   */
  protected function getCurrentField() {
    $fields = array_filter($this->parameters
      ->get('fields', []), function ($field) {
      return isset($field['settings_edit_form']['third_party_settings']['ds']['ft']['id']) && $field['settings_edit_form']['third_party_settings']['ds']['ft']['id'] == 'pattern';
    });
    $fields = array_keys($fields);
    $field = reset($fields);
    if (empty($field)) {
      $trigger_element = $this->parameters
        ->get('_triggering_element_name');
      $field = str_replace('_plugin_settings_edit', '', $trigger_element);
    }
    return $field;
  }

  /**
   * Pattern Display Suite field template plugin only supports actual fields.
   *
   * @param array $context
   *   Current context.
   *
   * @return bool
   *   TRUE if supported, FALSE otherwise.
   */
  protected function isSupportedField(array $context) {

    /** @var \Drupal\field\Entity\FieldConfig $field */
    if ($context['entity_type'] && $context['bundle']) {
      $field = $this->fieldManager
        ->getFieldDefinitions($context['entity_type'], $context['bundle']);
      return isset($field[$context['field_name']]);
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Pattern::$fieldManager protected property Entity field manager service.
Pattern::$moduleHandler protected property Module Handler.
Pattern::$parameters protected property Current $_POST parameters.
Pattern::$patternsManager protected property UI Patterns manager.
Pattern::$sourceManager protected property UI Patterns manager.
Pattern::alterForm public function
Pattern::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
Pattern::defaultConfiguration public function
Pattern::getContext protected function Get source field plugin context.
Pattern::getCurrentField protected function Get name of field currently being edited.
Pattern::isSupportedField protected function Pattern Display Suite field template plugin only supports actual fields.
Pattern::__construct public function Pattern constructor.
PatternDisplayFormTrait::buildPatternDisplayForm public function Build pattern display form.
PatternDisplayFormTrait::getDefaultValue protected function Helper function: get default value.
PatternDisplayFormTrait::getMappingDestination public function Helper function: return mapping destination given plugin id and field name.
PatternDisplayFormTrait::getMappingForm public function Get mapping form.
PatternDisplayFormTrait::hasMappingDestination public function Helper function: check if given source field has mapping destination.
PatternDisplayFormTrait::processFormStateValues public static function Normalize settings coming from a form submission.