/** * Asserts that a context for the given property path can be derived. * * @param \Drupal\Core\Entity\ContentEntityInterface $entity * The entity to test with. * @param $property_path * The property path to look for. * @param $expected_data_type * The expected data type. * * @return \Drupal\Core\Plugin\Context\ContextInterface * The context with a value. */ protected function assertPropertyPath(ContentEntityInterface $entity, $property_path, $expected_data_type) { $typed_data_entity = $entity->getTypedData(); $context_definition = new ContextDefinition($typed_data_entity->getDataDefinition()->getDataType()); $context_with_value = new Context($context_definition, $typed_data_entity); $context_without_value = new Context($context_definition); // Test the context without value. $property_context = $this->typedDataResolver->getContextFromProperty($property_path, $context_without_value); $this->assertEquals($expected_data_type, $property_context->getContextDefinition()->getDataType()); // Test the context with value. $property_context = $this->typedDataResolver->getContextFromProperty($property_path, $context_with_value); $this->assertEquals($expected_data_type, $property_context->getContextDefinition()->getDataType()); // Return the context with value so it can be asserted. return $property_context; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, $context = NULL, $tempstore_id = NULL, $machine_name = NULL) { $this->tempstore_id = $tempstore_id; $this->machine_name = $machine_name; $cached_values = $this->tempstore->get($this->tempstore_id)->get($this->machine_name); /** @var \Drupal\Core\Plugin\Context\ContextInterface[] $contexts */ $contexts = $this->getContexts($cached_values); $context_object = $this->resolver->convertTokenToContext($context, $contexts); $form['id'] = ['#type' => 'value', '#value' => $context]; $form['context_object'] = ['#type' => 'value', '#value' => $context_object]; $form['label'] = ['#type' => 'textfield', '#title' => $this->t('Context label'), '#default_value' => !empty($contexts[$context]) ? $contexts[$context]->getContextDefinition()->getLabel() : $this->resolver->getLabelByToken($context, $contexts), '#required' => TRUE]; $form['context_data'] = ['#type' => 'item', '#title' => $this->t('Data type'), '#markup' => $context_object->getContextDefinition()->getDataType()]; $form['submit'] = ['#type' => 'submit', '#value' => $this->t('Save'), '#ajax' => ['callback' => [$this, 'ajaxSave']]]; return $form; }