/**
  * {@inheritdoc}
  */
 public function getRelationship()
 {
     $plugin_definition = $this->getPluginDefinition();
     $context_definition = new ContextDefinition("language", $plugin_definition['label']);
     $context_value = NULL;
     // If the 'base' context has a value, then get the property value to put on
     // the context (otherwise, mapping hasn't occurred yet and we just want to
     // return the context with the right definition and no value).
     if ($this->getContext('base')->hasContextValue()) {
         $context_value = $this->getData($this->getContext('base'))->language;
     }
     $context_definition->setDefaultValue($context_value);
     return new Context($context_definition, $context_value);
 }
 /**
  * {@inheritdoc}
  */
 protected function generateDerivativeDefinition($base_plugin_definition, $data_type_id, $data_type_definition, DataDefinitionInterface $base_definition, $property_name, DataDefinitionInterface $property_definition)
 {
     $bundle_info = $base_definition->getConstraint('Bundle');
     // Identify base definitions that appear on bundle-able entities.
     if ($bundle_info && array_filter($bundle_info) && $base_definition->getConstraint('EntityType')) {
         $base_data_type = 'entity:' . $base_definition->getConstraint('EntityType');
     } else {
         $base_data_type = $data_type_id;
     }
     // If we've not processed this thing before.
     if (!isset($this->derivatives[$base_data_type . ':' . $property_name])) {
         $derivative = $base_plugin_definition;
         $derivative['label'] = $this->t($this->label, ['@property' => $property_definition->getLabel(), '@base' => $data_type_definition['label']]);
         $derivative['data_type'] = $property_definition->getFieldStorageDefinition()->getPropertyDefinition($property_definition->getFieldStorageDefinition()->getMainPropertyName())->getDataType();
         $derivative['property_name'] = $property_name;
         $context_definition = new ContextDefinition($base_data_type, $this->typedDataManager->createDataDefinition($base_data_type));
         // Add the constraints of the base definition to the context definition.
         if ($base_definition->getConstraint('Bundle')) {
             $context_definition->addConstraint('Bundle', $base_definition->getConstraint('Bundle'));
         }
         $derivative['context'] = ['base' => $context_definition];
         $derivative['property_name'] = $property_name;
         $this->derivatives[$base_data_type . ':' . $property_name] = $derivative;
     } elseif ($property_definition instanceof FieldConfigInterface) {
         // We should only end up in here on entity bundles.
         $derivative = $this->derivatives[$base_data_type . ':' . $property_name];
         // Update label
         /** @var \Drupal\Core\StringTranslation\TranslatableMarkup $label */
         $label = $derivative['label'];
         list(, , $argument_name) = explode(':', $data_type_id);
         $arguments = $label->getArguments();
         $arguments['@' . $argument_name] = $data_type_definition['label'];
         $string_args = $arguments;
         array_shift($string_args);
         $last = array_slice($string_args, -1);
         // The slice doesn't remove, so do that now.
         array_pop($string_args);
         $string = count($string_args) >= 2 ? '@property from ' . implode(', ', array_keys($string_args)) . ' and ' . array_keys($last)[0] : '@property from @base and ' . array_keys($last)[0];
         $this->derivatives[$base_data_type . ':' . $property_name]['label'] = $this->t($string, $arguments);
         if ($base_definition->getConstraint('Bundle')) {
             // Add bundle constraints
             $context_definition = $derivative['context']['base'];
             $bundles = $context_definition->getConstraint('Bundle') ?: [];
             $bundles = array_merge($bundles, $base_definition->getConstraint('Bundle'));
             $context_definition->addConstraint('Bundle', $bundles);
         }
     }
 }
示例#3
0
 /**
  * Tests basic context definition and value getters and setters.
  */
 function testContext()
 {
     $this->installEntitySchema('user');
     $name = $this->randomMachineName();
     $manager = new MockBlockManager();
     $plugin = $manager->createInstance('user_name');
     // Create a node, add it as context, catch the exception.
     $node = entity_create('node', array('title' => $name, 'type' => 'page'));
     // Try to get context that is missing its definition.
     try {
         $plugin->getContextDefinition('not_exists');
         $this->fail('The user context should not yet be set.');
     } catch (ContextException $e) {
         $this->assertEqual($e->getMessage(), 'The not_exists context is not a valid context.');
     }
     // Test the getContextDefinitions() method.
     $user_context_definition = ContextDefinition::create('entity:user')->setLabel(t('User'));
     $this->assertEqual($plugin->getContextDefinitions()['user']->getLabel(), $user_context_definition->getLabel());
     // Test the getContextDefinition() method for a valid context.
     $this->assertEqual($plugin->getContextDefinition('user')->getLabel(), $user_context_definition->getLabel());
     // Try to get a context with valid definition.
     $this->assertNotNull($plugin->getContext('user'), 'Succeeded to get a context with a valid definition.');
     // Try to get a value of a valid context, while this value has not been set.
     try {
         $plugin->getContextValue('user');
     } catch (ContextException $e) {
         $this->assertIdentical("The 'entity:user' context is required and not present.", $e->getMessage(), 'Requesting a non-set value of a required context should throw a context exception.');
     }
     // Try to pass the wrong class type as a context value.
     $plugin->setContextValue('user', $node);
     $violations = $plugin->validateContexts();
     $this->assertTrue(!empty($violations), 'The provided context value does not pass validation.');
     // Set an appropriate context value and check to make sure its methods work
     // as expected.
     $user = entity_create('user', array('name' => $name));
     $plugin->setContextValue('user', $user);
     $this->assertEqual($plugin->getContextValue('user')->getUsername(), $user->getUsername());
     $this->assertEqual($user->label(), $plugin->getTitle());
     // Test Optional context handling.
     $plugin = $manager->createInstance('user_name_optional');
     $this->assertNull($plugin->getContextValue('user'), 'Requesting a non-set value of a valid context should return NULL.');
     // Test Complex compound context handling.
     $complex_plugin = $manager->createInstance('complex_context');
     $complex_plugin->setContextValue('user', $user);
     // With only the user context set, try to get the context values.
     $values = $complex_plugin->getContextValues();
     $this->assertNull($values['node'], 'The node context is not yet set.');
     $this->assertNotNull($values['user'], 'The user context is set');
     $complex_plugin->setContextValue('node', $node);
     $context_wrappers = $complex_plugin->getContexts();
     // Make sure what came out of the wrappers is good.
     $this->assertEqual($context_wrappers['user']->getContextValue()->label(), $user->label());
     $this->assertEqual($context_wrappers['node']->getContextValue()->label(), $node->label());
     // Make sure what comes out of the context values is good.
     $contexts = $complex_plugin->getContextValues();
     $this->assertEqual($contexts['user']->label(), $user->label());
     $this->assertEqual($contexts['node']->label(), $node->label());
     // Test the title method for the complex context plugin.
     $this->assertEqual($user->label() . ' -- ' . $node->label(), $complex_plugin->getTitle());
 }
 /**
  * {@inheritdoc}
  */
 public function getDerivativeDefinitions($base_plugin_definition)
 {
     foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
         // Only allow content entities and ignore configuration entities.
         if (!$entity_type instanceof ContentEntityTypeInterface) {
             continue;
         }
         $this->derivatives["entity:{$entity_type_id}"] = ['label' => $this->t('Create @entity_type path alias', ['@entity_type' => $entity_type->getLowercaseLabel()]), 'category' => $this->t('Path'), 'entity_type_id' => $entity_type_id, 'context' => ['entity' => ContextDefinition::create("entity:{$entity_type_id}")->setLabel($entity_type->getLabel())->setRequired(TRUE)->setDescription($this->t('The @entity_type for which to create a path alias.', ['@entity_type' => $entity_type->getLowercaseLabel()])), 'alias' => ContextDefinition::create('string')->setLabel($this->t('Path alias'))->setRequired(TRUE)->setDescription($this->t("Specify an alternative path by which the content can be accessed. For example, 'about' for an about page. Use a relative path and do not add a trailing slash."))], 'provides' => []] + $base_plugin_definition;
     }
     return $this->derivatives;
 }
示例#5
0
 /**
  * {@inheritdoc}
  */
 public function getDerivativeDefinitions($base_plugin_definition)
 {
     foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
         // Only allow content entities and ignore configuration entities.
         if (!$entity_type instanceof ContentEntityTypeInterface) {
             continue;
         }
         $this->derivatives["entity:{$entity_type_id}"] = ['label' => $this->t('Create a new @entity_type', ['@entity_type' => $entity_type->getLowercaseLabel()]), 'category' => $entity_type->getLabel(), 'entity_type_id' => $entity_type_id, 'context' => [], 'provides' => ['entity' => ContextDefinition::create("entity:{$entity_type_id}")->setLabel($entity_type->getLabel())->setRequired(TRUE)]] + $base_plugin_definition;
         // Add a required context for the bundle key, and optional contexts for
         // other required base fields. This matches the storage create() behavior,
         // where only the bundle requirement is enforced.
         $bundle_key = $entity_type->getKey('bundle');
         $base_field_definitions = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id);
         foreach ($base_field_definitions as $field_name => $definition) {
             if ($field_name != $bundle_key && !$definition->isRequired()) {
                 continue;
             }
             $required = $field_name == $bundle_key;
             $multiple = $definition->getCardinality() === 1 ? FALSE : TRUE;
             $this->derivatives["entity:{$entity_type_id}"]['context'][$field_name] = ContextDefinition::create($definition->getType())->setLabel($definition->getLabel())->setRequired($required)->setMultiple($multiple)->setDescription($definition->getDescription());
         }
     }
     return $this->derivatives;
 }
示例#6
0
 /**
  * @covers ::getDefaultValue
  * @covers ::setDefaultValue
  */
 public function testDefaultValue()
 {
     $context_definition = new ContextDefinition();
     $this->assertNull($context_definition->getDefaultValue());
     $context_definition->setDefaultValue('test');
     $this->assertSame('test', $context_definition->getDefaultValue());
 }
示例#7
0
 /**
  * Provides data for testGetMatchingContexts().
  */
 public function providerTestGetMatchingContexts()
 {
     $requirement_any = new ContextDefinition();
     $requirement_specific = new ContextDefinition('specific');
     $requirement_specific->setConstraints(array('bar' => 'baz'));
     $context_any = $this->getMock('Drupal\\Core\\Plugin\\Context\\ContextInterface');
     $context_any->expects($this->atLeastOnce())->method('getContextDefinition')->will($this->returnValue(new ContextDefinition('empty')));
     $context_constraint_mismatch = $this->getMock('Drupal\\Core\\Plugin\\Context\\ContextInterface');
     $context_constraint_mismatch->expects($this->atLeastOnce())->method('getContextDefinition')->will($this->returnValue(new ContextDefinition('foo')));
     $context_datatype_mismatch = $this->getMock('Drupal\\Core\\Plugin\\Context\\ContextInterface');
     $context_datatype_mismatch->expects($this->atLeastOnce())->method('getContextDefinition')->will($this->returnValue(new ContextDefinition('fuzzy')));
     $context_definition_specific = new ContextDefinition('specific');
     $context_definition_specific->setConstraints(array('bar' => 'baz'));
     $context_specific = $this->getMock('Drupal\\Core\\Plugin\\Context\\ContextInterface');
     $context_specific->expects($this->atLeastOnce())->method('getContextDefinition')->will($this->returnValue($context_definition_specific));
     $data = array();
     // No context will return no valid contexts.
     $data[] = array(array(), $requirement_any);
     // A context with a generic matching requirement is valid.
     $data[] = array(array($context_any), $requirement_any);
     // A context with a specific matching requirement is valid.
     $data[] = array(array($context_specific), $requirement_specific);
     // A context with a mismatched constraint is invalid.
     $data[] = array(array($context_constraint_mismatch), $requirement_specific, array());
     // A context with a mismatched datatype is invalid.
     $data[] = array(array($context_datatype_mismatch), $requirement_specific, array());
     return $data;
 }