Issue #2257769 by tstoeckler | sun: Adding an Entity Reference field in the Field UI throws a PHP notice; fails to add field.
							parent
							
								
									39edc2708e
								
							
						
					
					
						commit
						ac31f29da3
					
				| 
						 | 
				
			
			@ -477,8 +477,12 @@ class FieldDefinition extends ListDataDefinition implements FieldDefinitionInter
 | 
			
		|||
      $definition = \Drupal::service('plugin.manager.field.field_type')->getDefinition($this->getType());
 | 
			
		||||
      $class = $definition['class'];
 | 
			
		||||
      $schema = $class::schema($this);
 | 
			
		||||
      // Fill in default values for optional entries.
 | 
			
		||||
      $schema += array('indexes' => array(), 'foreign keys' => array());
 | 
			
		||||
      // Fill in default values.
 | 
			
		||||
      $schema += array(
 | 
			
		||||
        'columns' => array(),
 | 
			
		||||
        'indexes' => array(),
 | 
			
		||||
        'foreign keys' => array(),
 | 
			
		||||
      );
 | 
			
		||||
 | 
			
		||||
      // Check that the schema does not include forbidden column names.
 | 
			
		||||
      if (array_intersect(array_keys($schema['columns']), static::getReservedColumns())) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -10,7 +10,7 @@ namespace Drupal\Core\Field;
 | 
			
		|||
use Drupal\Component\Plugin\Factory\DefaultFactory;
 | 
			
		||||
use Drupal\Core\Cache\CacheBackendInterface;
 | 
			
		||||
use Drupal\Core\Extension\ModuleHandlerInterface;
 | 
			
		||||
use Drupal\Core\Language\LanguageManager;
 | 
			
		||||
use Drupal\Core\Language\LanguageManagerInterface;
 | 
			
		||||
use Drupal\Core\Plugin\DefaultPluginManager;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
| 
						 | 
				
			
			@ -26,12 +26,12 @@ class FieldTypePluginManager extends DefaultPluginManager implements FieldTypePl
 | 
			
		|||
   *   keyed by the corresponding namespace to look for plugin implementations.
 | 
			
		||||
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
 | 
			
		||||
   *   Cache backend instance to use.
 | 
			
		||||
   * @param \Drupal\Core\Language\LanguageManager $language_manager
 | 
			
		||||
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
 | 
			
		||||
   *   The language manager.
 | 
			
		||||
   * @param \Drupal\Core\Extension\ModuleHandlerInterface
 | 
			
		||||
   *   The module handler.
 | 
			
		||||
   */
 | 
			
		||||
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManager $language_manager, ModuleHandlerInterface $module_handler) {
 | 
			
		||||
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManagerInterface $language_manager, ModuleHandlerInterface $module_handler) {
 | 
			
		||||
    parent::__construct('Plugin/Field/FieldType', $namespaces, $module_handler, 'Drupal\Core\Field\Annotation\FieldType');
 | 
			
		||||
    $this->alterInfo('field_info');
 | 
			
		||||
    $this->setCacheBackend($cache_backend, $language_manager, 'field_types_plugins');
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -75,7 +75,7 @@ class AnnotatedClassDiscovery extends ComponentAnnotatedClassDiscovery {
 | 
			
		|||
      $reader = parent::getAnnotationReader();
 | 
			
		||||
 | 
			
		||||
      // Add the Core annotation classes like @Translation.
 | 
			
		||||
      $reader->addNamespace('Drupal\Core\Annotation', array(DRUPAL_ROOT . '/core/lib/Drupal/Core/Annotation'));
 | 
			
		||||
      $reader->addNamespace('Drupal\Core\Annotation', array(dirname(dirname(__DIR__)) . '/Annotation'));
 | 
			
		||||
      $this->annotationReader = $reader;
 | 
			
		||||
    }
 | 
			
		||||
    return $this->annotationReader;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -17,9 +17,12 @@ class EntityReferenceAdminTest extends WebTestBase {
 | 
			
		|||
  /**
 | 
			
		||||
   * Modules to enable.
 | 
			
		||||
   *
 | 
			
		||||
   * Enable path module to ensure that the selection handler does not fail for
 | 
			
		||||
   * entities with a path field.
 | 
			
		||||
   *
 | 
			
		||||
   * @var array
 | 
			
		||||
   */
 | 
			
		||||
  public static $modules = array('node', 'field_ui', 'entity_reference');
 | 
			
		||||
  public static $modules = array('node', 'field_ui', 'entity_reference', 'path');
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * {@inheritdoc}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,58 @@
 | 
			
		|||
<?php
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @file
 | 
			
		||||
 * Contains \Drupal\path\Tests\Plugin\Field\FieldType\PathFieldDefinitionTest
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
namespace Drupal\path\Tests\Field;
 | 
			
		||||
 | 
			
		||||
use Drupal\Tests\Core\Field\FieldDefinitionTestBase;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Tests a field definition for a 'path' field.
 | 
			
		||||
 *
 | 
			
		||||
 * @see \Drupal\Core\Field\FieldDefinition
 | 
			
		||||
 * @see \Drupal\path\Plugin\Field\FieldType\PathItem
 | 
			
		||||
 *
 | 
			
		||||
 * @group Drupal
 | 
			
		||||
 * @group path
 | 
			
		||||
 */
 | 
			
		||||
class PathFieldDefinitionTest extends FieldDefinitionTestBase {
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * {@inheritdoc}
 | 
			
		||||
   */
 | 
			
		||||
  public static function getInfo() {
 | 
			
		||||
    return array(
 | 
			
		||||
      'name' => 'Path field definitions',
 | 
			
		||||
      'description' => 'Tests that field definitions for path fields work correctly.',
 | 
			
		||||
      'group' => 'Path',
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * {@inheritdoc}
 | 
			
		||||
   */
 | 
			
		||||
  protected function getPluginId() {
 | 
			
		||||
    return 'path';
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * {@inheritdoc}
 | 
			
		||||
   */
 | 
			
		||||
  protected function getNamespacePath() {
 | 
			
		||||
    return dirname(dirname(dirname(__DIR__))) . '/lib/Drupal/path';
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Tests FieldDefinition::getColumns().
 | 
			
		||||
   *
 | 
			
		||||
   * @covers \Drupal\Core\Field\FieldDefinition::getColumns
 | 
			
		||||
   * @covers \Drupal\path\Plugin\Field\FieldType\PathItem::getSchema
 | 
			
		||||
   */
 | 
			
		||||
  public function testGetColumns() {
 | 
			
		||||
    $this->assertSame(array(), $this->definition->getColumns());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,86 @@
 | 
			
		|||
<?php
 | 
			
		||||
/**
 | 
			
		||||
 * @file
 | 
			
		||||
 * Contains \Drupal\Tests\Core\Fied\FieldDefinitionTestBase.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
namespace Drupal\Tests\Core\Field;
 | 
			
		||||
 | 
			
		||||
use Drupal\Core\DependencyInjection\ContainerBuilder;
 | 
			
		||||
use Drupal\Core\Field\FieldDefinition;
 | 
			
		||||
use Drupal\Core\Field\FieldTypePluginManager;
 | 
			
		||||
use Drupal\Core\Language\Language;
 | 
			
		||||
use Drupal\Tests\UnitTestCase;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Provides a test base class for testing field definitions.
 | 
			
		||||
 */
 | 
			
		||||
abstract class FieldDefinitionTestBase extends UnitTestCase {
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * The field definition used in this test.
 | 
			
		||||
   *
 | 
			
		||||
   * @var \Drupal\Core\Field\FieldDefinition
 | 
			
		||||
   */
 | 
			
		||||
  protected $definition;
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * {@inheritdoc}
 | 
			
		||||
   */
 | 
			
		||||
  public function setUp() {
 | 
			
		||||
    $namespace_path = $this->getNamespacePath();
 | 
			
		||||
    // Suppport both PSR-0 and PSR-4 directory layouts.
 | 
			
		||||
    $module_name = basename($namespace_path);
 | 
			
		||||
    if ($module_name == 'src') {
 | 
			
		||||
      $module_name = basename($module_name);
 | 
			
		||||
    }
 | 
			
		||||
    $namespaces = new \ArrayObject(array(
 | 
			
		||||
      'Drupal\\' . $module_name => $namespace_path,
 | 
			
		||||
    ));
 | 
			
		||||
 | 
			
		||||
    $language_manager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
 | 
			
		||||
    $language_manager->expects($this->once())
 | 
			
		||||
      ->method('getCurrentLanguage')
 | 
			
		||||
      ->will($this->returnValue(new Language(array('id' => 'en'))));
 | 
			
		||||
    $module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
 | 
			
		||||
    $module_handler->expects($this->once())
 | 
			
		||||
      ->method('moduleExists')
 | 
			
		||||
      ->with($module_name)
 | 
			
		||||
      ->will($this->returnValue(TRUE));
 | 
			
		||||
    $plugin_manager = new FieldTypePluginManager(
 | 
			
		||||
      $namespaces,
 | 
			
		||||
      $this->getMock('Drupal\Core\Cache\CacheBackendInterface'),
 | 
			
		||||
      $language_manager,
 | 
			
		||||
      $module_handler
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    $container = new ContainerBuilder();
 | 
			
		||||
    $container->set('plugin.manager.field.field_type', $plugin_manager);
 | 
			
		||||
    // The 'string_translation' service is used by the @Translation annotation.
 | 
			
		||||
    $container->set('string_translation', $this->getStringTranslationStub());
 | 
			
		||||
    \Drupal::setContainer($container);
 | 
			
		||||
 | 
			
		||||
    $this->definition = FieldDefinition::create($this->getPluginId());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Returns the plugin ID of the tested field type.
 | 
			
		||||
   *
 | 
			
		||||
   * @return string
 | 
			
		||||
   *   The plugin ID.
 | 
			
		||||
   */
 | 
			
		||||
  abstract protected function getPluginId();
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Returns the path to the module's classes.
 | 
			
		||||
   *
 | 
			
		||||
   * Depending on whether the module follows the PSR-0 or PSR-4 directory layout
 | 
			
		||||
   * this should be either /path/to/module/lib/Drupal/mymodule or
 | 
			
		||||
   * /path/to/module/src.
 | 
			
		||||
   *
 | 
			
		||||
   * @return string
 | 
			
		||||
   *   The path to the module's classes.
 | 
			
		||||
   */
 | 
			
		||||
  abstract protected function getNamespacePath();
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue