Chris@0: getContextDefinitions() as $context_slot => $definition) { Chris@0: $valid_contexts = $this->contextHandler()->getMatchingContexts($contexts, $definition); Chris@0: $options = []; Chris@0: foreach ($valid_contexts as $context_id => $context) { Chris@0: $element['#tree'] = TRUE; Chris@0: $options[$context_id] = $context->getContextDefinition()->getLabel(); Chris@0: $element[$context_slot] = [ Chris@0: '#type' => 'value', Chris@0: '#value' => $context_id, Chris@0: ]; Chris@0: } Chris@0: Chris@17: // Show the context selector only if there is more than 1 option to choose Chris@17: // from. Also, show if there is a single option but the plugin does not Chris@17: // require a context. Chris@17: if (count($options) > 1 || (count($options) == 1 && !$definition->isRequired())) { Chris@0: $assignments = $plugin->getContextMapping(); Chris@0: $element[$context_slot] = [ Chris@0: '#title' => $definition->getLabel() ?: $this->t('Select a @context value:', ['@context' => $context_slot]), Chris@0: '#type' => 'select', Chris@0: '#options' => $options, Chris@0: '#required' => $definition->isRequired(), Chris@0: '#default_value' => !empty($assignments[$context_slot]) ? $assignments[$context_slot] : '', Chris@0: '#description' => $definition->getDescription(), Chris@0: ]; Chris@0: if (!$definition->isRequired()) { Chris@0: $element[$context_slot]['#empty_value'] = ''; Chris@0: } Chris@0: } Chris@0: } Chris@0: return $element; Chris@0: } Chris@0: Chris@0: }