Exemplo n.º 1
0
 /**
  * @covers ::setContextValue
  */
 public function testSetContextValueTypedData()
 {
     $this->contextDefinition = $this->getMockBuilder('Drupal\\Core\\Plugin\\Context\\ContextDefinitionInterface')->setMethods(array('getDefaultValue', 'getDataDefinition'))->getMockForAbstractClass();
     $typed_data = $this->getMock('Drupal\\Core\\TypedData\\TypedDataInterface');
     $context = new Context($this->contextDefinition, $typed_data);
     $this->assertSame($typed_data, $context->getContextData());
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state, $context_id = 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);
     $contexts = $this->getContexts($cached_values);
     $edit = FALSE;
     if (!empty($contexts[$context_id])) {
         $context = $contexts[$context_id];
         $machine_name = $context_id;
         $edit = TRUE;
     } else {
         $context_definition = new ContextDefinition($context_id);
         $context = new Context($context_definition);
         $machine_name = '';
     }
     $label = $context->getContextDefinition()->getLabel();
     $description = $context->getContextDefinition()->getDescription();
     $data_type = $context->getContextDefinition()->getDataType();
     $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
     $form['context_id'] = ['#type' => 'value', '#value' => $context_id];
     $form['label'] = ['#type' => 'textfield', '#title' => $this->t('Label'), '#default_value' => $label, '#required' => TRUE];
     $form['machine_name'] = ['#type' => 'machine_name', '#title' => $this->t('Machine Name'), '#default_value' => $machine_name, '#required' => TRUE, '#maxlength' => 128, '#machine_name' => ['source' => ['label'], 'exists' => [$this, 'contextExists']], '#disabled' => $this->disableMachineName($cached_values, $machine_name)];
     $form['description'] = ['#type' => 'textarea', '#title' => $this->t('Description'), '#default_value' => $description];
     if (strpos($data_type, 'entity:') === 0) {
         list(, $entity_type) = explode(':', $data_type);
         /** @var EntityAdapter $entity */
         $entity = $edit ? $context->getContextValue() : NULL;
         $form['context_value'] = ['#type' => 'entity_autocomplete', '#required' => TRUE, '#target_type' => $entity_type, '#default_value' => $entity, '#title' => $this->t('Select entity')];
     } else {
         $value = $context->getContextData()->getValue();
         $form['context_value'] = ['#title' => $this->t('Set a context value'), '#type' => 'textfield', '#required' => TRUE, '#default_value' => $value];
     }
     $form['submit'] = ['#type' => 'submit', '#value' => $this->t('Save'), '#ajax' => ['callback' => [$this, 'ajaxSave']]];
     return $form;
 }
Exemplo n.º 3
0
 /**
  * @covers ::setContextValue
  */
 public function testSetContextValueCacheableDependency()
 {
     $container = new Container();
     $cache_context_manager = $this->getMockBuilder('Drupal\\Core\\Cache\\CacheContextsManager')->disableOriginalConstructor()->getMock();
     $container->set('cache_contexts_manager', $cache_context_manager);
     $cache_context_manager->expects($this->any())->method('validateTokens')->with(['route'])->willReturn(['route']);
     \Drupal::setContainer($container);
     $this->contextDefinition = $this->getMock('Drupal\\Core\\Plugin\\Context\\ContextDefinitionInterface');
     $context = new Context($this->contextDefinition);
     $context->setTypedDataManager($this->typedDataManager);
     $cacheable_dependency = $this->getMock('Drupal\\Tests\\Core\\Plugin\\Context\\TypedDataCacheableDependencyInterface');
     $cacheable_dependency->expects($this->once())->method('getCacheTags')->willReturn(['node:1']);
     $cacheable_dependency->expects($this->once())->method('getCacheContexts')->willReturn(['route']);
     $cacheable_dependency->expects($this->once())->method('getCacheMaxAge')->willReturn(60);
     $context->setContextValue($cacheable_dependency);
     $this->assertSame($cacheable_dependency, $context->getContextData());
     $this->assertEquals(['node:1'], $context->getCacheTags());
     $this->assertEquals(['route'], $context->getCacheContexts());
     $this->assertEquals(60, $context->getCacheMaxAge());
 }
Exemplo n.º 4
0
 /**
  * @covers ::getContextData
  */
 public function testDefaultDataValue()
 {
     $context = new Context($this->contextDefinition);
     $context->setTypedDataManager($this->typedDataManager);
     $this->assertEquals($this->typedData, $context->getContextData());
 }