diff --git a/rdf_builder/src/Form/ContentBuilderForm.php b/rdf_builder/src/Form/ContentBuilderForm.php index 6d2001a..eafbd99 100644 --- a/rdf_builder/src/Form/ContentBuilderForm.php +++ b/rdf_builder/src/Form/ContentBuilderForm.php @@ -7,6 +7,7 @@ namespace Drupal\rdf_builder\Form; +use Drupal\Component\Utility\Html; use Drupal\Component\Utility\String; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; @@ -64,8 +65,8 @@ class ContentBuilderForm extends FormBase { public function __construct() { $this->converter = new SchemaOrgConverter(); $this->datatype_field_mappings = array( - 'http://schema.org/Text' => 'text', - 'http://schema.org/PostalAddress' => 'text_long', + 'http://schema.org/Text' => 'string', + 'http://schema.org/PostalAddress' => 'string_long', 'http://schema.org/Number' => 'integer', 'http://schema.org/MediaObject' => 'file', 'http://schema.org/AudioObject' => 'file', @@ -90,15 +91,15 @@ class ContentBuilderForm extends FormBase { */ public function nextSubmit(array &$form, FormStateInterface &$form_state) { - $form_state->set(['page_values','1'],$form_state->getValues()); + $form_state->set(['page_values', 1], $form_state->getValues()); - if (!is_null($form_state->get(['page_values','2']))) { - $form_state->setValues($form_state->get(['page_values','2'])); + if ($form_state->has(['page_values', 2])) { + $form_state->setValues($form_state->get(['page_values', 2])); } // When form rebuilds, build method would be chosen based on to page_num. $form_state->set('page_num', 2); - $form_state->setRebuild(TRUE); + $form_state->setRebuild(); } /** @@ -113,13 +114,13 @@ class ContentBuilderForm extends FormBase { */ public function buildForm(array $form, FormStateInterface $form_state) { - // Display page 2 if $form_state['page_num'] == 2. - if (!is_null($form_state->get('page_num')) && $form_state->get('page_num')== 2) { + // Display page 2 if $form_state->get('page_num') == 2. + if ($form_state->has('page_num') && $form_state->get('page_num') == 2) { return $this->buildFormPageTwo($form, $form_state); } // Otherwise build page 1. - $form_state->set('page_num',1); + $form_state->set('page_num', 1); $form['#title'] = $this->t('Content types'); $form['description'] = array( @@ -134,14 +135,11 @@ class ContentBuilderForm extends FormBase { '#required' => TRUE, '#options' => $this->converter->getListTypes(), '#empty_option' => '', - '#default_value' => !is_null($form_state->getValue('rdf-type')) ? $form_state->getValue('rdf-type') : '', + '#default_value' => $form_state->getValue('rdf-type', ''), '#attached' => array( 'library' => array( 'rdfui/drupal.rdfui.autocomplete', ), - 'css' => array( - drupal_get_path('module', 'rdfui') . '/css/rdfui.autocomplete.css', - ), ), '#description' => $this->t('Specify the type you want to associated to this content type e.g. Article, Blog, etc.'), ); @@ -175,7 +173,7 @@ class ContentBuilderForm extends FormBase { '#title' => $this->t('Choose fields to start with.'), ); - $rdf_type = $form_state->get(['page_values',1,'rdf-type']); + $rdf_type = $form_state->get(['page_values', 1, 'rdf-type']); $properties = $this->converter->getTypeProperties($rdf_type); $field_types = \Drupal::service('plugin.manager.field.field_type') ->getUiDefinitions(); @@ -200,14 +198,14 @@ class ContentBuilderForm extends FormBase { '#regions' => array(), '#attributes' => array( 'class' => array('rdfui-field-mappings'), - 'id' => drupal_html_id('rdf-builder'), + 'id' => Html::getId('rdf-builder'), ), ); foreach ($properties as $key => $value) { $table[$key] = array( '#attributes' => array( - 'id' => drupal_html_class($key), + 'id' => Html::getClass($key), ), 'enable' => array( '#type' => 'checkbox', @@ -290,7 +288,7 @@ class ContentBuilderForm extends FormBase { public function pageTwoBackSubmit(array &$form, FormStateInterface &$form_state) { $form_state->setValues($form_state->get(['page_values', 1])); $form_state->set('page_num', 1); - $form_state->setRebuild(TRUE); + $form_state->setRebuild(); } /** @@ -321,7 +319,7 @@ class ContentBuilderForm extends FormBase { } } - $page_one_values = $form_state->get(['page_values',1]); + $page_one_values = $form_state->get(['page_values', 1]); $rdf_type = $page_one_values['rdf-type']; $this->createNodeType($rdf_type); @@ -403,6 +401,7 @@ class ContentBuilderForm extends FormBase { try { entity_create('field_storage_config', $field_storage)->save(); entity_create('field_config', $instance)->save(); + // Make sure the field is displayed in the 'default' form mode (using // default widget and settings). It stays hidden for other form modes // until it is explicitly configured. @@ -470,6 +469,6 @@ class ContentBuilderForm extends FormBase { return $this->datatype_field_mappings[$datatype]; } } - return 'text'; + return 'string'; } } diff --git a/rdf_builder/src/Tests/ContentTypeBuilderTest.php b/rdf_builder/src/Tests/ContentTypeBuilderTest.php index 48f549e..ee5e824 100644 --- a/rdf_builder/src/Tests/ContentTypeBuilderTest.php +++ b/rdf_builder/src/Tests/ContentTypeBuilderTest.php @@ -26,7 +26,7 @@ class ContentTypeBuilderTest extends WebTestBase { 'field', 'node', 'field_ui', - ); + ); /** * {@inheritdoc} @@ -77,12 +77,7 @@ class ContentTypeBuilderTest extends WebTestBase { $this->assertText("Create field: you need to provide a data type for name", 'Form validated and errors displayed.'); $this->assertUrl($this->uri, array(), 'Stayed on same page after incorrect submission.'); - $edit = array( - 'fields[schema:email][enable]' => '1', - 'fields[schema:email][type]' => 'email', - 'fields[schema:name][enable]' => '1', - 'fields[schema:name][type]' => 'text', - ); + $edit['fields[schema:name][type]'] = 'text'; $this->drupalPostForm(NULL, $edit, t('Save')); //$this->assertUrl('admin/structure/types', array(), 'Redirected to correct url upon correct submission.'); diff --git a/rdfui.libraries.yml b/rdfui.libraries.yml index 89e1403..baf0b31 100644 --- a/rdfui.libraries.yml +++ b/rdfui.libraries.yml @@ -2,9 +2,9 @@ drupal.rdfui.autocomplete: version: VERSION js: js/rdfui.autocomplete.js: {} -# css: -# component: -# css/rdfui.autocomplete.css: {} + css: + theme: + css/rdfui.autocomplete.css: {} dependencies: - core/drupal.autocomplete - core/jquery diff --git a/src/ContentMappings.php b/src/ContentMappings.php index 1eb04d2..13dc65d 100644 --- a/src/ContentMappings.php +++ b/src/ContentMappings.php @@ -47,9 +47,6 @@ class ContentMappings { 'library' => array( 'rdfui/drupal.rdfui.autocomplete', ), - 'css' => array( - drupal_get_path('module', 'rdfui') . '/css/rdfui.autocomplete.css', - ), ), '#default_value' => !empty($existing_type) ? $existing_type['types'][0] : '', '#description' => t('Specify the type you want to associated to this content type e.g. Article, Blog, etc.'), @@ -70,14 +67,14 @@ class ContentMappings { * Saves Schema.org mappings in \Drupal\node\NodeTypeForm. */ public static function submitForm(array &$form, FormStateInterface $form_state) { - if (!is_null($form_state->getValue('types'))) { + if ($form_state->hasValue('types')) { $entity_type = $form_state->getFormObject()->getEntity(); $mapping = rdf_get_mapping('node', $entity_type->id()); if ($entity_type->isNew()) { - $mapping = rdf_get_mapping('node', $form_state->getValue('type')); + $mapping = rdf_get_mapping('node', $form_state->getValue('types')); } - if (!is_null($form_state->getValue('types'))) { + if ($form_state->hasValue('types')) { $mapping->setBundleMapping(array('types' => array($form_state->getValue('types')))) ->save(); } diff --git a/src/Form/FieldMappings.php b/src/Form/FieldMappings.php index fc6548e..f092192 100644 --- a/src/Form/FieldMappings.php +++ b/src/Form/FieldMappings.php @@ -7,21 +7,25 @@ namespace Drupal\rdfui\Form; +use Drupal\Component\Plugin\PluginManagerBase; use Drupal\Component\Utility\String; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityManagerInterface; -use Drupal\Core\Field\FieldTypePluginManagerInterface; +use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Field\FieldTypePluginManager; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\Element; use Drupal\field\FieldConfigInterface; -use Drupal\field_ui\OverviewBase; +use Drupal\field_ui\FormDisplayOverview; use Drupal\rdfui\EasyRdfConverter; use Drupal\rdfui\SchemaOrgConverter; use Symfony\Component\DependencyInjection\ContainerInterface; + /** * RDF UI Field Mapping form. */ -class FieldMappings extends OverviewBase { +class FieldMappings extends FormDisplayOverview{ /** * The field type manager. @@ -42,11 +46,17 @@ class FieldMappings extends OverviewBase { * * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager. - * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager + * @param \Drupal\Core\Field\FieldTypePluginManager $field_type_manager * The field type manager. + * @param \Drupal\Component\Plugin\PluginManagerBase $plugin_manager + * The widget or formatter plugin manager. + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler + * The module handler to use for invoking hooks. + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The configuration factory. */ - public function __construct(EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager) { - parent::__construct($entity_manager); + public function __construct(EntityManagerInterface $entity_manager, FieldTypePluginManager $field_type_manager, PluginManagerBase $plugin_manager, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory) { + parent::__construct($entity_manager, $field_type_manager, $plugin_manager, $module_handler, $config_factory); $this->fieldTypeManager = $field_type_manager; $this->rdfConverter = new SchemaOrgConverter(); } @@ -57,7 +67,10 @@ class FieldMappings extends OverviewBase { public static function create(ContainerInterface $container) { return new static( $container->get('entity.manager'), - $container->get('plugin.manager.field.field_type') + $container->get('plugin.manager.field.field_type'), + $container->get('plugin.manager.field.widget'), + $container->get('module_handler'), + $container->get('config.factory') ); } @@ -135,9 +148,6 @@ class FieldMappings extends OverviewBase { 'library' => array( 'rdfui/drupal.rdfui.autocomplete', ), - 'css' => array( - drupal_get_path('module', 'rdfui') . '/css/rdfui.autocomplete.css', - ), ), '#default_value' => !empty($property) ? $property['properties'][0] : '', ), @@ -192,7 +202,7 @@ class FieldMappings extends OverviewBase { } /** - * Overrides \Drupal\field_ui\OverviewBase::submitForm(). + * Overrides \Drupal\field_ui\FormDisplayOverview::submitForm(). */ public function submitForm(array &$form, FormStateInterface $form_state) { diff --git a/src/Plugin/Derivative/RdfUiLocalTask.php b/src/Plugin/Derivative/RdfUiLocalTask.php index 478dfad..9be5251 100644 --- a/src/Plugin/Derivative/RdfUiLocalTask.php +++ b/src/Plugin/Derivative/RdfUiLocalTask.php @@ -101,7 +101,7 @@ class RdfUiLocalTask extends DeriverBase implements ContainerDeriverInterface { */ public function alterLocalTasks(array &$local_tasks) { foreach ($this->entityManager->getDefinitions() as $entity_type => $entity_info) { - if ($entity_info->isFieldable() && $entity_info->hasLinkTemplate('admin-form')) { + if ($entity_type->get('field_ui_base_route')) { $admin_form = $entity_info->getLinkTemplate('admin-form'); $local_tasks["field_ui.fields:rdf_$entity_type"]['base_route'] = $admin_form;