class FieldTemplateProcessor in UI Patterns 8
Class FieldTemplateProcessor.
@package Drupal\ui_patterns_ds
Hierarchy
- class \Drupal\ui_patterns_ds\FieldTemplateProcessor implements FieldTemplateProcessorInterface
Expanded class hierarchy of FieldTemplateProcessor
1 string reference to 'FieldTemplateProcessor'
- ui_patterns_ds.services.yml in modules/
ui_patterns_ds/ ui_patterns_ds.services.yml - modules/ui_patterns_ds/ui_patterns_ds.services.yml
1 service uses FieldTemplateProcessor
- ui_patterns_ds.field_template_processor in modules/
ui_patterns_ds/ ui_patterns_ds.services.yml - Drupal\ui_patterns_ds\FieldTemplateProcessor
File
- modules/
ui_patterns_ds/ src/ FieldTemplateProcessor.php, line 12
Namespace
Drupal\ui_patterns_dsView source
class FieldTemplateProcessor implements FieldTemplateProcessorInterface {
/**
* Variables array.
*
* @var array
*/
protected $variables = [];
/**
* {@inheritdoc}
*/
public function process(array &$variables) {
$this->variables = $variables;
$content = [];
foreach ($variables['items'] as $delta => $item) {
$fields = [];
foreach ($this
->getMapping() as $mapping) {
$fields[$mapping['destination']][] = $this
->getSourceValue($mapping, $delta);
}
$content['pattern_' . $delta] = [
'#type' => 'pattern',
'#id' => $this
->getPatternId(),
'#variant' => $this
->getVariant(),
'#fields' => $fields,
'#context' => $this
->getContext(),
'#multiple_sources' => TRUE,
];
}
$variables['pattern'] = $content;
}
/**
* Get source value.
*
* @param array $mapping
* Mapping array.
* @param int $delta
* Field delta.
*
* @return mixed
* Source value.
*/
public function getSourceValue(array $mapping, $delta) {
$value = $this->variables['items'][$delta]['content'];
if ($mapping['source'] != $this
->getFieldName()) {
$column = $this
->getColumnName($mapping['source']);
$value = $this
->getEntity()
->get($this
->getFieldName())
->getValue();
$value = $value[$delta][$column];
}
return $value;
}
/**
* Get field parent entity.
*
* @return \Drupal\Core\Entity\ContentEntityBase
* Parent entity.
*/
protected function getEntity() {
return $this->variables['element']['#object'];
}
/**
* Get Pattern ID.
*
* @return string
* Pattern ID.
*/
protected function getPatternId() {
return $this
->getSetting('pattern');
}
/**
* Get mapping settings.
*
* @return array
* Mapping settings.
*/
protected function getMapping() {
return $this
->getSetting('pattern_mapping', []);
}
/**
* Get mapping settings.
*
* @return string
* Mapping settings.
*/
protected function getVariant() {
return $this
->getSetting('pattern_variant');
}
/**
* Get setting value or default to given value if none set.
*
* @param string $name
* Setting name.
* @param string $default
* Setting default value.
*
* @return mixed
* Setting value.
*/
protected function getSetting($name, $default = '') {
return isset($this->variables['ds-config']['settings'][$name]) ? $this->variables['ds-config']['settings'][$name] : $default;
}
/**
* Get field name.
*
* @return string
* Field name.
*/
protected function getFieldName() {
return $this->variables['field_name'];
}
/**
* Extract column name from a source name.
*
* @param string $source
* Source name.
*
* @return string
* Column name.
*/
protected function getColumnName($source) {
return str_replace($this
->getFieldName() . '__', '', $source);
}
/**
* Get pattern context.
*
* @return array
* Pattern context.
*/
protected function getContext() {
$element = $this->variables['element'];
$context = [
'type' => 'ds_field_template',
'field_name' => $this
->getFieldName(),
'entity_type' => $element['#entity_type'],
'bundle' => $element['#bundle'],
'view_mode' => $element['#view_mode'],
'entity' => NULL,
];
if (isset($element['#object']) && is_object($element['#object']) && $element['#object'] instanceof ContentEntityBase) {
$context['entity'] = $element['#object'];
}
return $context;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FieldTemplateProcessor:: |
protected | property | Variables array. | |
FieldTemplateProcessor:: |
protected | function | Extract column name from a source name. | |
FieldTemplateProcessor:: |
protected | function | Get pattern context. | |
FieldTemplateProcessor:: |
protected | function | Get field parent entity. | |
FieldTemplateProcessor:: |
protected | function | Get field name. | |
FieldTemplateProcessor:: |
protected | function | Get mapping settings. | |
FieldTemplateProcessor:: |
protected | function | Get Pattern ID. | |
FieldTemplateProcessor:: |
protected | function | Get setting value or default to given value if none set. | |
FieldTemplateProcessor:: |
public | function | Get source value. | |
FieldTemplateProcessor:: |
protected | function | Get mapping settings. | |
FieldTemplateProcessor:: |
public | function |
Process field template variables. Overrides FieldTemplateProcessorInterface:: |