/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $vocabulary = $this->entity; if ($vocabulary->isNew()) { $form['#title'] = $this->t('Add vocabulary'); } else { $form['#title'] = $this->t('Edit vocabulary'); } $form['name'] = array('#type' => 'textfield', '#title' => $this->t('Name'), '#default_value' => $vocabulary->label(), '#maxlength' => 255, '#required' => TRUE); $form['vid'] = array('#type' => 'machine_name', '#default_value' => $vocabulary->id(), '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH, '#machine_name' => array('exists' => array($this, 'exists'), 'source' => array('name'))); $form['description'] = array('#type' => 'textfield', '#title' => $this->t('Description'), '#default_value' => $vocabulary->getDescription()); // $form['langcode'] is not wrapped in an // if ($this->moduleHandler->moduleExists('language')) check because the // language_select form element works also without the language module being // installed. https://www.drupal.org/node/1749954 documents the new element. $form['langcode'] = array('#type' => 'language_select', '#title' => $this->t('Vocabulary language'), '#languages' => LanguageInterface::STATE_ALL, '#default_value' => $vocabulary->language()->getId()); if ($this->moduleHandler->moduleExists('language')) { $form['default_terms_language'] = array('#type' => 'details', '#title' => $this->t('Terms language'), '#open' => TRUE); $form['default_terms_language']['default_language'] = array('#type' => 'language_configuration', '#entity_information' => array('entity_type' => 'taxonomy_term', 'bundle' => $vocabulary->id()), '#default_value' => ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', $vocabulary->id())); } // Set the hierarchy to "multiple parents" by default. This simplifies the // vocabulary form and standardizes the term form. $form['hierarchy'] = array('#type' => 'value', '#value' => '0'); $form = parent::form($form, $form_state); return $this->protectBundleIdElement($form); }
/** * Tests migration of content language settings when there is no language lock. */ public function testLanguageContentWithNoLanguageLock() { // Assert that a we can assign a language. $config = ContentLanguageSettings::loadByEntityTypeBundle('node', 'employee'); $this->assertSame($config->getDefaultLangcode(), 'current_interface'); $this->assertTrue($config->isLanguageAlterable()); }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); $comment_type = $this->entity; $form['label'] = array('#type' => 'textfield', '#title' => t('Label'), '#maxlength' => 255, '#default_value' => $comment_type->label(), '#required' => TRUE); $form['id'] = array('#type' => 'machine_name', '#default_value' => $comment_type->id(), '#machine_name' => array('exists' => '\\Drupal\\comment\\Entity\\CommentType::load'), '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH, '#disabled' => !$comment_type->isNew()); $form['description'] = array('#type' => 'textarea', '#default_value' => $comment_type->getDescription(), '#description' => t('Describe this comment type. The text will be displayed on the <em>Comment types</em> administration overview page.'), '#title' => t('Description')); if ($comment_type->isNew()) { $options = array(); foreach ($this->entityManager->getDefinitions() as $entity_type) { // Only expose entities that have field UI enabled, only those can // get comment fields added in the UI. if ($entity_type->get('field_ui_base_route')) { $options[$entity_type->id()] = $entity_type->getLabel(); } } $form['target_entity_type_id'] = array('#type' => 'select', '#default_value' => $comment_type->getTargetEntityTypeId(), '#title' => t('Target entity type'), '#options' => $options, '#description' => t('The target entity type can not be changed after the comment type has been created.')); } else { $form['target_entity_type_id_display'] = array('#type' => 'item', '#markup' => $this->entityManager->getDefinition($comment_type->getTargetEntityTypeId())->getLabel(), '#title' => t('Target entity type')); } if ($this->moduleHandler->moduleExists('content_translation')) { $form['language'] = array('#type' => 'details', '#title' => t('Language settings'), '#group' => 'additional_settings'); $language_configuration = ContentLanguageSettings::loadByEntityTypeBundle('comment', $comment_type->id()); $form['language']['language_configuration'] = array('#type' => 'language_configuration', '#entity_information' => array('entity_type' => 'comment', 'bundle' => $comment_type->id()), '#default_value' => $language_configuration); $form['#submit'][] = 'language_configuration_element_submit'; } $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save')); return $form; }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); /** @var \Drupal\commerce_product\Entity\ProductTypeInterface $product_type */ $product_type = $this->entity; $variation_types = $this->variationTypeStorage->loadMultiple(); $variation_types = array_map(function ($variation_type) { return $variation_type->label(); }, $variation_types); // Create an empty product to get the default status value. // @todo Clean up once https://www.drupal.org/node/2318187 is fixed. if ($this->operation == 'add') { $product = $this->entityTypeManager->getStorage('commerce_product')->create(['type' => $product_type->uuid()]); } else { $product = $this->entityTypeManager->getStorage('commerce_product')->create(['type' => $product_type->id()]); } $form['label'] = ['#type' => 'textfield', '#title' => $this->t('Label'), '#maxlength' => 255, '#default_value' => $product_type->label(), '#required' => TRUE]; $form['id'] = ['#type' => 'machine_name', '#default_value' => $product_type->id(), '#machine_name' => ['exists' => '\\Drupal\\commerce_product\\Entity\\ProductType::load'], '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH]; $form['description'] = ['#type' => 'textfield', '#title' => $this->t('Description'), '#default_value' => $product_type->getDescription()]; $form['variationType'] = ['#type' => 'select', '#title' => $this->t('Product variation type'), '#default_value' => $product_type->getVariationType(), '#options' => $variation_types, '#required' => TRUE]; $form['product_status'] = ['#type' => 'checkbox', '#title' => t('Publish new products of this type by default.'), '#default_value' => $product->isPublished()]; if ($this->moduleHandler->moduleExists('language')) { $form['language'] = ['#type' => 'details', '#title' => $this->t('Language settings'), '#group' => 'additional_settings']; $form['language']['language_configuration'] = ['#type' => 'language_configuration', '#entity_information' => ['entity_type' => 'commerce_product', 'bundle' => $product_type->id()], '#default_value' => ContentLanguageSettings::loadByEntityTypeBundle('commerce_product', $product_type->id())]; $form['#submit'][] = 'language_configuration_element_submit'; } return $this->protectBundleIdElement($form); }
protected function setUp() { parent::setUp(); // Set up an additional language. $this->langcodes = array(\Drupal::languageManager()->getDefaultLanguage()->getId(), 'es'); ConfigurableLanguage::createFromLangcode('es')->save(); // Create a content type. $this->bundle = $this->randomMachineName(); $this->contentType = $this->drupalCreateContentType(array('type' => $this->bundle)); // Enable translation for the current entity type and ensure the change is // picked up. \Drupal::service('content_translation.manager')->setEnabled('node', $this->bundle, TRUE); drupal_static_reset(); \Drupal::entityManager()->clearCachedBundles(); \Drupal::service('router.builder')->rebuild(); // Add a translatable field to the content type. entity_create('field_storage_config', array('field_name' => 'field_test_text', 'entity_type' => 'node', 'type' => 'text', 'cardinality' => 1, 'translatable' => TRUE))->save(); entity_create('field_config', array('entity_type' => 'node', 'field_name' => 'field_test_text', 'bundle' => $this->bundle, 'label' => 'Test text-field'))->save(); entity_get_form_display('node', $this->bundle, 'default')->setComponent('field_test_text', array('type' => 'text_textfield', 'weight' => 0))->save(); // Enable content translation. ContentLanguageSettings::loadByEntityTypeBundle('node', $this->bundle)->setLanguageAlterable(TRUE)->setDefaultLangcode(\Drupal::languageManager()->getDefaultLanguage()->getId())->save(); // Create a translator user. $permissions = array('access contextual links', 'administer nodes', "edit any {$this->bundle} content", 'translate any entity'); $this->translator = $this->drupalCreateUser($permissions); }
/** * Tests term language settings for vocabulary terms are saved and updated. */ function testVocabularyDefaultLanguageForTerms() { // Add a new vocabulary and check that the default language settings are for // the terms are saved. $edit = array('name' => $this->randomMachineName(), 'vid' => Unicode::strtolower($this->randomMachineName()), 'default_language[langcode]' => 'bb', 'default_language[language_alterable]' => TRUE); $vid = $edit['vid']; $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save')); // Check that the vocabulary was actually created. $this->drupalGet('admin/structure/taxonomy/manage/' . $edit['vid']); $this->assertResponse(200, 'The vocabulary has been created.'); // Check that the language settings were saved. $language_settings = ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', $edit['vid']); $this->assertEqual($language_settings->getDefaultLangcode(), 'bb', 'The langcode was saved.'); $this->assertTrue($language_settings->isLanguageAlterable(), 'The visibility setting was saved.'); // Check that the correct options are selected in the interface. $this->assertOptionSelected('edit-default-language-langcode', 'bb', 'The correct default language for the terms of this vocabulary is selected.'); $this->assertFieldChecked('edit-default-language-language-alterable', 'Show language selection option is checked.'); // Edit the vocabulary and check that the new settings are updated. $edit = array('default_language[langcode]' => 'aa', 'default_language[language_alterable]' => FALSE); $this->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save')); // And check again the settings and also the interface. $language_settings = ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', $vid); $this->assertEqual($language_settings->getDefaultLangcode(), 'aa', 'The langcode was saved.'); $this->assertFalse($language_settings->isLanguageAlterable(), 'The visibility setting was saved.'); $this->drupalGet('admin/structure/taxonomy/manage/' . $vid); $this->assertOptionSelected('edit-default-language-langcode', 'aa', 'The correct default language for the terms of this vocabulary is selected.'); $this->assertNoFieldChecked('edit-default-language-language-alterable', 'Show language selection option is not checked.'); // Check that language settings are changed after editing vocabulary. $edit = array('name' => $this->randomMachineName(), 'default_language[langcode]' => 'authors_default', 'default_language[language_alterable]' => FALSE); $this->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save')); // Check that we have the new settings. $new_settings = ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', $vid); $this->assertEqual($new_settings->getDefaultLangcode(), 'authors_default', 'The langcode was saved.'); $this->assertFalse($new_settings->isLanguageAlterable(), 'The new visibility setting was saved.'); }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); /** @var \Drupal\commerce_product\Entity\ProductVariationTypeInterface $variation_type */ $variation_type = $this->entity; $form['label'] = ['#type' => 'textfield', '#title' => $this->t('Label'), '#maxlength' => 255, '#default_value' => $variation_type->label(), '#required' => TRUE]; $form['id'] = ['#type' => 'machine_name', '#default_value' => $variation_type->id(), '#machine_name' => ['exists' => '\\Drupal\\commerce_product\\Entity\\ProductVariationType::load'], '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH]; $form['generateTitle'] = ['#type' => 'checkbox', '#title' => t('Generate variation titles based on attribute values.'), '#default_value' => $variation_type->shouldGenerateTitle()]; if (\Drupal::moduleHandler()->moduleExists('commerce_order')) { // Prepare a list of line item types used to purchase product variations. $line_item_type_storage = $this->entityTypeManager->getStorage('commerce_line_item_type'); $line_item_types = $line_item_type_storage->loadMultiple(); $line_item_types = array_filter($line_item_types, function ($line_item_type) { return $line_item_type->getPurchasableEntityTypeId() == 'commerce_product_variation'; }); $line_item_types = array_map(function ($line_item_type) { return $line_item_type->label(); }, $line_item_types); $form['lineItemType'] = ['#type' => 'select', '#title' => $this->t('Line item type'), '#default_value' => $variation_type->getLineItemTypeId(), '#options' => $line_item_types, '#empty_value' => '', '#required' => TRUE]; } if ($this->moduleHandler->moduleExists('language')) { $form['language'] = ['#type' => 'details', '#title' => $this->t('Language settings'), '#group' => 'additional_settings']; $form['language']['language_configuration'] = ['#type' => 'language_configuration', '#entity_information' => ['entity_type' => 'commerce_product_variation', 'bundle' => $variation_type->id()], '#default_value' => ContentLanguageSettings::loadByEntityTypeBundle('commerce_product_variation', $variation_type->id())]; $form['#submit'][] = 'language_configuration_element_submit'; } return $this->protectBundleIdElement($form); }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $conf = ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'some_bundle'); $form['lang_configuration'] = array('#type' => 'language_configuration', '#entity_information' => array('entity_type' => 'entity_test', 'bundle' => 'some_bundle'), '#default_value' => $conf); $form['submit'] = array('#type' => 'submit', '#value' => 'Save'); $form['#submit'][] = 'language_configuration_element_submit'; return $form; }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); $store_type = $this->entity; $form['label'] = ['#type' => 'textfield', '#title' => $this->t('Label'), '#maxlength' => 255, '#default_value' => $store_type->label(), '#description' => $this->t('Label for the store type.'), '#required' => TRUE]; $form['id'] = ['#type' => 'machine_name', '#default_value' => $store_type->id(), '#machine_name' => ['exists' => '\\Drupal\\commerce_store\\Entity\\StoreType::load'], '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH]; $form['description'] = ['#type' => 'textfield', '#title' => $this->t('Description'), '#default_value' => $store_type->getDescription()]; if ($this->moduleHandler->moduleExists('language')) { $form['language'] = ['#type' => 'details', '#title' => $this->t('Language settings'), '#group' => 'additional_settings']; $form['language']['language_configuration'] = ['#type' => 'language_configuration', '#entity_information' => ['entity_type' => 'commerce_store', 'bundle' => $store_type->id()], '#default_value' => ContentLanguageSettings::loadByEntityTypeBundle('commerce_store', $store_type->id())]; $form['#submit'][] = 'language_configuration_element_submit'; } return $this->protectBundleIdElement($form); }
/** * Tests migration of content language settings. */ public function testLanguageContent() { // Assert that a translatable content is still translatable. $config = $this->config('language.content_settings.node.blog'); $this->assertIdentical($config->get('target_entity_type_id'), 'node'); $this->assertIdentical($config->get('target_bundle'), 'blog'); $this->assertIdentical($config->get('default_langcode'), 'current_interface'); $this->assertFalse($config->get('language_alterable')); $this->assertTrue($config->get('third_party_settings.content_translation.enabled')); // Assert that a non-translatable content is not translatable. $config = ContentLanguageSettings::loadByEntityTypeBundle('node', 'page'); $this->assertTrue($config->isDefaultConfiguration()); $this->assertFalse($config->isLanguageAlterable()); $this->assertSame($config->getDefaultLangcode(), 'site_default'); }
/** * Tests menu language settings and the defaults for menu link items. */ function testMenuLanguage() { // Create a test menu to test the various language-related settings. // Machine name has to be lowercase. $menu_name = Unicode::strtolower($this->randomMachineName(16)); $label = $this->randomString(); $edit = array('id' => $menu_name, 'description' => '', 'label' => $label, 'langcode' => 'aa'); $this->drupalPostForm('admin/structure/menu/add', $edit, t('Save')); ContentLanguageSettings::loadByEntityTypeBundle('menu_link_content', 'menu_link_content')->setDefaultLangcode('bb')->setLanguageAlterable(TRUE)->save(); // Check menu language. $this->assertOptionSelected('edit-langcode', $edit['langcode'], 'The menu language was correctly selected.'); // Test menu link language. $link_path = '/'; // Add a menu link. $link_title = $this->randomString(); $edit = array('title[0][value]' => $link_title, 'link[0][uri]' => $link_path); $this->drupalPostForm("admin/structure/menu/manage/{$menu_name}/add", $edit, t('Save')); // Check the link was added with the correct menu link default language. $menu_links = entity_load_multiple_by_properties('menu_link_content', array('title' => $link_title)); $menu_link = reset($menu_links); $this->assertMenuLink($menu_link->getPluginId(), array('menu_name' => $menu_name, 'route_name' => '<front>', 'langcode' => 'bb')); // Edit menu link default, changing it to cc. ContentLanguageSettings::loadByEntityTypeBundle('menu_link_content', 'menu_link_content')->setDefaultLangcode('cc')->setLanguageAlterable(TRUE)->save(); // Add a menu link. $link_title = $this->randomString(); $edit = array('title[0][value]' => $link_title, 'link[0][uri]' => $link_path); $this->drupalPostForm("admin/structure/menu/manage/{$menu_name}/add", $edit, t('Save')); // Check the link was added with the correct new menu link default language. $menu_links = entity_load_multiple_by_properties('menu_link_content', array('title' => $link_title)); $menu_link = reset($menu_links); $this->assertMenuLink($menu_link->getPluginId(), array('menu_name' => $menu_name, 'route_name' => '<front>', 'langcode' => 'cc')); // Now change the language of the new link to 'bb'. $edit = array('langcode[0][value]' => 'bb'); $this->drupalPostForm('admin/structure/menu/item/' . $menu_link->id() . '/edit', $edit, t('Save')); $this->assertMenuLink($menu_link->getPluginId(), array('menu_name' => $menu_name, 'route_name' => '<front>', 'langcode' => 'bb')); // Saving menu link items ends up on the edit menu page. To check the menu // link has the correct language default on edit, go to the menu link edit // page first. $this->drupalGet('admin/structure/menu/item/' . $menu_link->id() . '/edit'); // Check that the language selector has the correct default value. $this->assertOptionSelected('edit-langcode-0-value', 'bb', 'The menu link language was correctly selected.'); // Edit menu to hide the language select on menu link item add. ContentLanguageSettings::loadByEntityTypeBundle('menu_link_content', 'menu_link_content')->setDefaultLangcode('cc')->setLanguageAlterable(FALSE)->save(); // Check that the language selector is not available on menu link add page. $this->drupalGet("admin/structure/menu/manage/{$menu_name}/add"); $this->assertNoField('edit-langcode-0-value', 'The language selector field was hidden the page'); }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); /* @var \Drupal\block_content\BlockContentTypeInterface $block_type */ $block_type = $this->entity; $form['label'] = array('#type' => 'textfield', '#title' => t('Label'), '#maxlength' => 255, '#default_value' => $block_type->label(), '#description' => t("Provide a label for this block type to help identify it in the administration pages."), '#required' => TRUE); $form['id'] = array('#type' => 'machine_name', '#default_value' => $block_type->id(), '#machine_name' => array('exists' => '\\Drupal\\block_content\\Entity\\BlockContentType::load'), '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH, '#disabled' => !$block_type->isNew()); $form['description'] = array('#type' => 'textarea', '#default_value' => $block_type->getDescription(), '#description' => t('Enter a description for this block type.'), '#title' => t('Description')); $form['revision'] = array('#type' => 'checkbox', '#title' => t('Create new revision'), '#default_value' => $block_type->shouldCreateNewRevision(), '#description' => t('Create a new revision by default for this block type.')); if ($this->moduleHandler->moduleExists('language')) { $form['language'] = array('#type' => 'details', '#title' => t('Language settings'), '#group' => 'additional_settings'); $language_configuration = ContentLanguageSettings::loadByEntityTypeBundle('block_content', $block_type->id()); $form['language']['language_configuration'] = array('#type' => 'language_configuration', '#entity_information' => array('entity_type' => 'block_content', 'bundle' => $block_type->id()), '#default_value' => $language_configuration); $form['#submit'][] = 'language_configuration_element_submit'; } $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save')); return $form; }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); $type = $this->entity; if ($this->operation == 'add') { $form['#title'] = $this->t('Add content type'); $fields = $this->entityManager->getBaseFieldDefinitions('node'); // Create a node with a fake bundle using the type's UUID so that we can // get the default values for workflow settings. // @todo Make it possible to get default values without an entity. // https://www.drupal.org/node/2318187 $node = $this->entityManager->getStorage('node')->create(array('type' => $type->uuid())); } else { $form['#title'] = $this->t('Edit %label content type', array('%label' => $type->label())); $fields = $this->entityManager->getFieldDefinitions('node', $type->id()); // Create a node to get the current values for workflow settings fields. $node = $this->entityManager->getStorage('node')->create(array('type' => $type->id())); } $form['name'] = array('#title' => t('Name'), '#type' => 'textfield', '#default_value' => $type->label(), '#description' => t('The human-readable name of this content type. This text will be displayed as part of the list on the <em>Add content</em> page. This name must be unique.'), '#required' => TRUE, '#size' => 30); $form['type'] = array('#type' => 'machine_name', '#default_value' => $type->id(), '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH, '#disabled' => $type->isLocked(), '#machine_name' => array('exists' => ['Drupal\\node\\Entity\\NodeType', 'load'], 'source' => array('name')), '#description' => t('A unique machine-readable name for this content type. It must only contain lowercase letters, numbers, and underscores. This name will be used for constructing the URL of the %node-add page, in which underscores will be converted into hyphens.', array('%node-add' => t('Add content')))); $form['description'] = array('#title' => t('Description'), '#type' => 'textarea', '#default_value' => $type->getDescription(), '#description' => t('This text will be displayed on the <em>Add new content</em> page.')); $form['additional_settings'] = array('#type' => 'vertical_tabs', '#attached' => array('library' => array('node/drupal.content_types'))); $form['submission'] = array('#type' => 'details', '#title' => t('Submission form settings'), '#group' => 'additional_settings', '#open' => TRUE); $form['submission']['title_label'] = array('#title' => t('Title field label'), '#type' => 'textfield', '#default_value' => $fields['title']->getLabel(), '#required' => TRUE); $form['submission']['preview_mode'] = array('#type' => 'radios', '#title' => t('Preview before submitting'), '#default_value' => $type->getPreviewMode(), '#options' => array(DRUPAL_DISABLED => t('Disabled'), DRUPAL_OPTIONAL => t('Optional'), DRUPAL_REQUIRED => t('Required'))); $form['submission']['help'] = array('#type' => 'textarea', '#title' => t('Explanation or submission guidelines'), '#default_value' => $type->getHelp(), '#description' => t('This text will be displayed at the top of the page when creating or editing content of this type.')); $form['workflow'] = array('#type' => 'details', '#title' => t('Publishing options'), '#group' => 'additional_settings'); $workflow_options = array('status' => $node->status->value, 'promote' => $node->promote->value, 'sticky' => $node->sticky->value, 'revision' => $type->isNewRevision()); // Prepare workflow options to be used for 'checkboxes' form element. $keys = array_keys(array_filter($workflow_options)); $workflow_options = array_combine($keys, $keys); $form['workflow']['options'] = array('#type' => 'checkboxes', '#title' => t('Default options'), '#default_value' => $workflow_options, '#options' => array('status' => t('Published'), 'promote' => t('Promoted to front page'), 'sticky' => t('Sticky at top of lists'), 'revision' => t('Create new revision')), '#description' => t('Users with the <em>Administer content</em> permission will be able to override these options.')); if ($this->moduleHandler->moduleExists('language')) { $form['language'] = array('#type' => 'details', '#title' => t('Language settings'), '#group' => 'additional_settings'); $language_configuration = ContentLanguageSettings::loadByEntityTypeBundle('node', $type->id()); $form['language']['language_configuration'] = array('#type' => 'language_configuration', '#entity_information' => array('entity_type' => 'node', 'bundle' => $type->id()), '#default_value' => $language_configuration); } $form['display'] = array('#type' => 'details', '#title' => t('Display settings'), '#group' => 'additional_settings'); $form['display']['display_submitted'] = array('#type' => 'checkbox', '#title' => t('Display author and date information'), '#default_value' => $type->displaySubmitted(), '#description' => t('Author username and publish date will be displayed.')); return $this->protectBundleIdElement($form); }
/** * Set up configuration for multiple languages. */ function setUpLanguages() { // Add languages. $this->defaultLanguage = 'en'; $this->secondaryLanguage = 'es'; $this->addLanguage($this->secondaryLanguage); // Display the language widget. $config = ContentLanguageSettings::loadByEntityTypeBundle('node', 'simplenews_issue'); $config->setLanguageAlterable(TRUE); $config->save(); // Make Simplenews issue translatable. \Drupal::service('content_translation.manager')->setEnabled('node', 'simplenews_issue', TRUE); drupal_static_reset(); \Drupal::entityManager()->clearCachedDefinitions(); \Drupal::service('router.builder')->rebuild(); \Drupal::service('entity.definition_update_manager')->applyUpdates(); // Make Simplenews issue body translatable. $field = FieldConfig::loadByName('node', 'simplenews_issue', 'body'); $field->setTranslatable(TRUE); $field->save(); $this->rebuildContainer(); }
/** * Creates a new node content type. * * @param string $name * The content type name. * @param string $langcode * Default language code of the nodes of this type. */ protected function createContentType($name, $langcode) { $content_type = $this->container->get('entity.manager')->getStorage('node_type')->create(array('name' => 'Test ' . $name, 'title_label' => 'Title', 'type' => $name, 'create_body' => FALSE)); $content_type->save(); ContentLanguageSettings::loadByEntityTypeBundle('node', $name)->setLanguageAlterable(FALSE)->setDefaultLangcode($langcode)->save(); }
/** * Exposes "pseudo-field" components on content entities. * * Field UI's "Manage fields" and "Manage display" pages let users re-order * fields, but also non-field components. For nodes, these include elements * exposed by modules through hook_form_alter(), for instance. * * Content entities or modules that want to have their components supported * should expose them using this hook. The user-defined settings (weight, * visible) are automatically applied when entities or entity forms are * rendered. * * @see hook_entity_extra_field_info_alter() * * @return array * The array structure is identical to that of the return value of * \Drupal\Core\Entity\EntityManagerInterface::getExtraFields(). */ function hook_entity_extra_field_info() { $extra = array(); $module_language_enabled = \Drupal::moduleHandler()->moduleExists('language'); $description = t('Node module element'); foreach (NodeType::loadMultiple() as $bundle) { // Add also the 'language' select if Language module is enabled and the // bundle has multilingual support. // Visibility of the ordering of the language selector is the same as on the // node/add form. if ($module_language_enabled) { $configuration = ContentLanguageSettings::loadByEntityTypeBundle('node', $bundle->type); if ($configuration->isLanguageAlterable()) { $extra['node'][$bundle->type]['form']['language'] = array('label' => t('Language'), 'description' => $description, 'weight' => 0); } } $extra['node'][$bundle->type]['display']['language'] = array('label' => t('Language'), 'description' => $description, 'weight' => 0, 'visible' => FALSE); } return $extra; }
/** * Tests that the settings UI works as expected. */ function testSettingsUI() { // Check for the content_translation_menu_links_discovered_alter() changes. $this->drupalGet('admin/config'); $this->assertLink('Content language and translation'); $this->assertText('Configure language and translation support for content.'); // Test that the translation settings are ignored if the bundle is marked // translatable but the entity type is not. $edit = array('settings[comment][comment_article][translatable]' => TRUE); $this->assertSettings('comment', NULL, FALSE, $edit); // Test that the translation settings are ignored if only a field is marked // as translatable and not the related entity type and bundle. $edit = array('settings[comment][comment_article][fields][comment_body]' => TRUE); $this->assertSettings('comment', NULL, FALSE, $edit); // Test that the translation settings are not stored if an entity type and // bundle are marked as translatable but no field is. $edit = array('entity_types[comment]' => TRUE, 'settings[comment][comment_article][translatable]' => TRUE, 'settings[comment][comment_article][fields][changed]' => FALSE, 'settings[comment][comment_article][fields][created]' => FALSE, 'settings[comment][comment_article][fields][homepage]' => FALSE, 'settings[comment][comment_article][fields][hostname]' => FALSE, 'settings[comment][comment_article][fields][mail]' => FALSE, 'settings[comment][comment_article][fields][name]' => FALSE, 'settings[comment][comment_article][fields][status]' => FALSE, 'settings[comment][comment_article][fields][subject]' => FALSE, 'settings[comment][comment_article][fields][uid]' => FALSE); $this->assertSettings('comment', 'comment_article', FALSE, $edit); $xpath_err = '//div[contains(@class, "error")]'; $this->assertTrue($this->xpath($xpath_err), 'Enabling translation only for entity bundles generates a form error.'); // Test that the translation settings are not stored if a non-configurable // language is set as default and the language selector is hidden. $edit = array('entity_types[comment]' => TRUE, 'settings[comment][comment_article][settings][language][langcode]' => Language::LANGCODE_NOT_SPECIFIED, 'settings[comment][comment_article][settings][language][language_alterable]' => FALSE, 'settings[comment][comment_article][translatable]' => TRUE, 'settings[comment][comment_article][fields][comment_body]' => TRUE); $this->assertSettings('comment', 'comment_article', FALSE, $edit); $this->assertTrue($this->xpath($xpath_err), 'Enabling translation with a fixed non-configurable language generates a form error.'); // Test that a field shared among different bundles can be enabled without // needing to make all the related bundles translatable. $edit = array('entity_types[comment]' => TRUE, 'settings[comment][comment_article][settings][language][langcode]' => 'current_interface', 'settings[comment][comment_article][settings][language][language_alterable]' => TRUE, 'settings[comment][comment_article][translatable]' => TRUE, 'settings[comment][comment_article][fields][comment_body]' => TRUE, 'settings[comment][comment_article][fields][subject]' => FALSE, 'settings[comment][comment][fields][subject]' => FALSE); $this->assertSettings('comment', 'comment_article', TRUE, $edit); $definition = $this->entityManager()->getFieldDefinitions('comment', 'comment_article')['comment_body']; $this->assertTrue($definition->isTranslatable(), 'Article comment body is translatable.'); $definition = $this->entityManager()->getFieldDefinitions('comment', 'comment_article')['subject']; $this->assertFalse($definition->isTranslatable(), 'Article comment subject is not translatable.'); $definition = $this->entityManager()->getFieldDefinitions('comment', 'comment')['comment_body']; $this->assertFalse($definition->isTranslatable(), 'Page comment body is not translatable.'); $definition = $this->entityManager()->getFieldDefinitions('comment', 'comment')['subject']; $this->assertFalse($definition->isTranslatable(), 'Page comment subject is not translatable.'); // Test that translation can be enabled for base fields. $edit = array('entity_types[entity_test_mul]' => TRUE, 'settings[entity_test_mul][entity_test_mul][translatable]' => TRUE, 'settings[entity_test_mul][entity_test_mul][fields][name]' => TRUE, 'settings[entity_test_mul][entity_test_mul][fields][user_id]' => FALSE); $this->assertSettings('entity_test_mul', 'entity_test_mul', TRUE, $edit); $field_override = BaseFieldOverride::loadByName('entity_test_mul', 'entity_test_mul', 'name'); $this->assertTrue($field_override->isTranslatable(), 'Base fields can be overridden with a base field bundle override entity.'); $definitions = $this->entityManager()->getFieldDefinitions('entity_test_mul', 'entity_test_mul'); $this->assertTrue($definitions['name']->isTranslatable() && !$definitions['user_id']->isTranslatable(), 'Base field bundle overrides were correctly altered.'); // Test that language settings are correctly stored. $language_configuration = ContentLanguageSettings::loadByEntityTypeBundle('comment', 'comment_article'); $this->assertEqual($language_configuration->getDefaultLangcode(), 'current_interface', 'The default language for article comments is set to the interface text language selected for page.'); $this->assertTrue($language_configuration->isLanguageAlterable(), 'The language selector for article comments is shown.'); // Verify language widget appears on comment type form. $this->drupalGet('admin/structure/comment/manage/comment_article'); $this->assertField('language_configuration[content_translation]'); $this->assertFieldChecked('edit-language-configuration-content-translation'); // Verify that translation may be enabled for the article content type. $edit = array('language_configuration[content_translation]' => TRUE); // Make sure the checkbox is available and not checked by default. $this->drupalGet('admin/structure/types/manage/article'); $this->assertField('language_configuration[content_translation]'); $this->assertNoFieldChecked('edit-language-configuration-content-translation'); $this->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type')); $this->drupalGet('admin/structure/types/manage/article'); $this->assertFieldChecked('edit-language-configuration-content-translation'); // Test that the title field of nodes is available in the settings form. $edit = array('entity_types[node]' => TRUE, 'settings[node][article][settings][language][langcode]' => 'current_interface', 'settings[node][article][settings][language][language_alterable]' => TRUE, 'settings[node][article][translatable]' => TRUE, 'settings[node][article][fields][title]' => TRUE); $this->assertSettings('node', NULL, TRUE, $edit); foreach (array(TRUE, FALSE) as $translatable) { // Test that configurable field translatability is correctly switched. $edit = array('settings[node][article][fields][body]' => $translatable); $this->assertSettings('node', 'article', TRUE, $edit); $field = FieldConfig::loadByName('node', 'article', 'body'); $definitions = \Drupal::entityManager()->getFieldDefinitions('node', 'article'); $this->assertEqual($definitions['body']->isTranslatable(), $translatable, 'Field translatability correctly switched.'); $this->assertEqual($field->isTranslatable(), $definitions['body']->isTranslatable(), 'Configurable field translatability correctly switched.'); // Test that also the Field UI form behaves correctly. $translatable = !$translatable; $edit = array('translatable' => $translatable); $this->drupalPostForm('admin/structure/types/manage/article/fields/node.article.body', $edit, t('Save settings')); \Drupal::entityManager()->clearCachedFieldDefinitions(); $field = FieldConfig::loadByName('node', 'article', 'body'); $definitions = \Drupal::entityManager()->getFieldDefinitions('node', 'article'); $this->assertEqual($definitions['body']->isTranslatable(), $translatable, 'Field translatability correctly switched.'); $this->assertEqual($field->isTranslatable(), $definitions['body']->isTranslatable(), 'Configurable field translatability correctly switched.'); } // Test that the order of the language list is similar to other language // lists, such as in Views UI. $this->drupalGet('admin/config/regional/content-language'); $expected_elements = array('site_default', 'current_interface', 'authors_default', 'en', 'und', 'zxx'); $elements = $this->xpath('//select[@id="edit-settings-node-article-settings-language-langcode"]/option'); // Compare values inside the option elements with expected values. for ($i = 0; $i < count($elements); $i++) { $this->assertEqual($elements[$i]->attributes()->{'value'}, $expected_elements[$i]); } }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); $productType = $this->entity; $variationTypes = $this->variationTypeStorage->loadMultiple(); $variationTypes = array_map(function($variationType) { return $variationType->label(); }, $variationTypes); $form['label'] = [ '#type' => 'textfield', '#title' => $this->t('Label'), '#maxlength' => 255, '#default_value' => $productType->label(), '#required' => TRUE, ]; $form['id'] = [ '#type' => 'machine_name', '#default_value' => $productType->id(), '#machine_name' => [ 'exists' => '\Drupal\commerce_product\Entity\ProductType::load', ], '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH, ]; $form['description'] = [ '#type' => 'textfield', '#title' => $this->t('Description'), '#default_value' => $productType->getDescription(), ]; $form['variationType'] = [ '#type' => 'select', '#title' => $this->t('Product variation type'), '#default_value' => $productType->getVariationType(), '#options' => $variationTypes, '#required' => TRUE, ]; $form['digital'] = [ '#type' => 'checkbox', '#title' => t('Digital'), '#default_value' => $productType->isDigital(), '#description' => t('Products of this type represent digital services.') ]; if ($this->moduleHandler->moduleExists('language')) { $form['language'] = [ '#type' => 'details', '#title' => $this->t('Language settings'), '#group' => 'additional_settings', ]; $form['language']['language_configuration'] = [ '#type' => 'language_configuration', '#entity_information' => [ 'entity_type' => 'commerce_product', 'bundle' => $productType->id(), ], '#default_value' => ContentLanguageSettings::loadByEntityTypeBundle('commerce_product', $productType->id()), ]; $form['#submit'][] = 'language_configuration_element_submit'; } return $this->protectBundleIdElement($form);; }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { foreach ($form_state->getValue('settings') as $entity_type => $entity_settings) { foreach ($entity_settings as $bundle => $bundle_settings) { $config = ContentLanguageSettings::loadByEntityTypeBundle($entity_type, $bundle); $config->setDefaultLangcode($bundle_settings['settings']['language']['langcode'])->setLanguageAlterable($bundle_settings['settings']['language']['language_alterable'])->save(); } } drupal_set_message($this->t('Settings successfully updated.')); }
/** * Loads a content language config entity based on the entity type and bundle. * * @param string $entity_type_id * ID of the entity type. * @param string $bundle * Bundle name. * * @return $this * The content language config entity if one exists. Otherwise, returns * default values. */ public static function loadByEntityTypeBundle($entity_type_id, $bundle) { if ($entity_type_id == NULL || $bundle == NULL) { return NULL; } $config = \Drupal::entityManager()->getStorage('language_content_settings')->load($entity_type_id . '.' . $bundle); if ($config == NULL) { $config = ContentLanguageSettings::create(['target_entity_type_id' => $entity_type_id, 'target_bundle' => $bundle]); } return $config; }
/** * Tests that the configuration is updated when a vocabulary is changed. */ public function testTaxonomyVocabularyUpdate() { $vocabulary = entity_create('taxonomy_vocabulary', array('name' => 'Country', 'vid' => 'country')); $vocabulary->save(); $admin_user = $this->drupalCreateUser(array('administer taxonomy')); $this->drupalLogin($admin_user); $edit = array('default_language[langcode]' => 'current_interface', 'default_language[language_alterable]' => TRUE); $this->drupalPostForm('admin/structure/taxonomy/manage/country', $edit, t('Save')); // Check the language default configuration. $configuration = ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', 'country'); $uuid = $configuration->uuid(); $this->assertEqual($configuration->getDefaultLangcode(), 'current_interface', 'The default language configuration has been saved on the Country vocabulary.'); $this->assertTrue($configuration->isLanguageAlterable(), 'The alterable language configuration has been saved on the Country vocabulary.'); // Rename the vocabulary. $edit = array('vid' => 'nation'); $this->drupalPostForm('admin/structure/taxonomy/manage/country', $edit, t('Save')); // Check that we still have the settings for the new vocabulary. $configuration = ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', 'nation'); $this->assertEqual($configuration->getDefaultLangcode(), 'current_interface', 'The default language configuration has been kept on the new Country vocabulary.'); $this->assertTrue($configuration->isLanguageAlterable(), 'The alterable language configuration has been kept on the new Country vocabulary.'); $this->assertEqual($configuration->uuid(), $uuid, 'The language configuration uuid has been kept on the new Country vocabulary.'); }
/** * Tests file usage with translated entities. */ public function testFileUsageWithEntityTranslation() { /** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */ $file_usage = $this->container->get('file.usage'); $this->enableModules(['node', 'language']); $this->installEntitySchema('node'); $this->installSchema('node', ['node_access']); // Activate English and Romanian languages. ConfigurableLanguage::create(['id' => 'en'])->save(); ConfigurableLanguage::create(['id' => 'ro'])->save(); NodeType::create(['type' => 'page'])->save(); ContentLanguageSettings::loadByEntityTypeBundle('node', 'page')->setLanguageAlterable(FALSE)->setDefaultLangcode('en')->save(); // Create a file field attached to 'page' node-type. FieldStorageConfig::create(['type' => 'file', 'entity_type' => 'node', 'field_name' => 'file'])->save(); FieldConfig::create(['entity_type' => 'node', 'bundle' => 'page', 'field_name' => 'file', 'label' => 'File'])->save(); // Create a node, attach a file and add a Romanian translation. $node = Node::create(['type' => 'page', 'title' => 'Page']); $node->set('file', $file = $this->createFile())->addTranslation('ro', $node->getTranslation('en')->toArray())->save(); // Check that the file is used twice. $usage = $file_usage->listUsage($file); $this->assertEquals(2, $usage['file']['node'][$node->id()]); // Remove the Romanian translation. $node->removeTranslation('ro'); $node->save(); // Check that one usage has been removed and is used only once now. $usage = $file_usage->listUsage($file); $this->assertEquals(1, $usage['file']['node'][$node->id()]); }
public function providerLoadByEntityTypeBundle() { $alteredLanguage = new ContentLanguageSettings(array('target_entity_type_id' => 'test_entity_type', 'target_bundle' => 'test_bundle'), 'language_content_settings'); $alteredLanguage->setLanguageAlterable(true); $langcode = $this->randomMachineName(); $alteredDefaultLangcode = new ContentLanguageSettings(array('target_entity_type_id' => 'test_entity_type', 'target_bundle' => 'test_fixed_language_bundle'), 'language_content_settings'); $alteredDefaultLangcode->setDefaultLangcode($langcode); $defaultConfig = new ContentLanguageSettings(array('target_entity_type_id' => 'test_entity_type', 'target_bundle' => 'test_default_language_bundle'), 'language_content_settings'); return [['test_entity_type.test_bundle', $alteredLanguage, LanguageInterface::LANGCODE_SITE_DEFAULT, true], ['test_entity_type.test_fixed_language_bundle', $alteredDefaultLangcode, $langcode, false], ['test_entity_type.test_default_language_bundle', $defaultConfig, LanguageInterface::LANGCODE_SITE_DEFAULT, false], ['test_entity_type.null_bundle', NULL, LanguageInterface::LANGCODE_SITE_DEFAULT, false]]; }