/** * {@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 = language_get_default_configuration('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) { $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->name, '#maxlength' => 255, '#required' => TRUE); $form['vid'] = array('#type' => 'machine_name', '#default_value' => $vocabulary->id(), '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH, '#machine_name' => array('exists' => 'taxonomy_vocabulary_load', 'source' => array('name'))); $form['description'] = array('#type' => 'textfield', '#title' => $this->t('Description'), '#default_value' => $vocabulary->description); // $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. http://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' => language_get_default_configuration('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'); return parent::form($form, $form_state, $vocabulary); }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); $type = $this->entity; if ($this->operation == 'add') { $form['#title'] = String::checkPlain($this->t('Add content type')); } elseif ($this->operation == 'edit') { $form['#title'] = $this->t('Edit %label content type', array('%label' => $type->label())); } $node_settings = $type->getModuleSettings('node'); // Prepare node options to be used for 'checkboxes' form element. $keys = array_keys(array_filter($node_settings['options'])); $node_settings['options'] = array_combine($keys, $keys); $form['name'] = array('#title' => t('Name'), '#type' => 'textfield', '#default_value' => $type->name, '#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. It is recommended that this name begin with a capital letter and contain only letters, numbers, and spaces. 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' => 'node_type_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->description, '#description' => t('Describe this content type. The text will be displayed on the <em>Add 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' => $type->title_label, '#required' => TRUE); $form['submission']['preview'] = array('#type' => 'radios', '#title' => t('Preview before submitting'), '#parents' => array('settings', 'node', 'preview'), '#default_value' => $node_settings['preview'], '#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->help, '#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'); $form['workflow']['options'] = array('#type' => 'checkboxes', '#title' => t('Default options'), '#parents' => array('settings', 'node', 'options'), '#default_value' => $node_settings['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 = language_get_default_configuration('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']['submitted'] = array('#type' => 'checkbox', '#title' => t('Display author and date information.'), '#parents' => array('settings', 'node', 'submitted'), '#default_value' => $node_settings['submitted'], '#description' => t('Author username and publish date will be displayed.')); return $form; }
/** * {@inheritdoc} */ public function form(array $form, array &$form_state) { $menu = $this->entity; if ($this->operation == 'edit') { $form['#title'] = $this->t('Edit menu %label', array('%label' => $menu->label())); } $form['label'] = array('#type' => 'textfield', '#title' => t('Title'), '#default_value' => $menu->label(), '#required' => TRUE); $form['id'] = array('#type' => 'machine_name', '#title' => t('Menu name'), '#default_value' => $menu->id(), '#maxlength' => MENU_MAX_MENU_NAME_LENGTH_UI, '#description' => t('A unique name to construct the URL for the menu. It must only contain lowercase letters, numbers and hyphens.'), '#machine_name' => array('exists' => array($this, 'menuNameExists'), 'source' => array('label'), 'replace_pattern' => '[^a-z0-9-]+', 'replace' => '-'), '#disabled' => !$menu->isNew() || $menu->isLocked()); $form['description'] = array('#type' => 'textfield', '#title' => t('Administrative summary'), '#maxlength' => 512, '#default_value' => $menu->description); $form['langcode'] = array('#type' => 'language_select', '#title' => t('Menu language'), '#languages' => LanguageInterface::STATE_ALL, '#default_value' => $menu->language()->getId()); // Unlike the menu langcode, the default language configuration for menu // links only works with language module installed. if ($this->moduleHandler->moduleExists('language')) { $form['default_menu_links_language'] = array('#type' => 'details', '#title' => t('Menu links language'), '#open' => TRUE); $form['default_menu_links_language']['default_language'] = array('#type' => 'language_configuration', '#entity_information' => array('entity_type' => 'menu_link', 'bundle' => $menu->id()), '#default_value' => language_get_default_configuration('menu_link', $menu->id())); } // Add menu links administration form for existing menus. if (!$menu->isNew() || $menu->isLocked()) { // Form API supports constructing and validating self-contained sections // within forms, but does not allow to handle the form section's submission // equally separated yet. Therefore, we use a $form_state key to point to // the parents of the form section. // @see self::submitOverviewForm() $form_state['menu_overview_form_parents'] = array('links'); $form['links'] = array(); $form['links'] = $this->buildOverviewForm($form['links'], $form_state); } return parent::form($form, $form_state); }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $conf = language_get_default_configuration('some_custom_type', 'some_bundle'); $form['lang_configuration'] = array('#type' => 'language_configuration', '#entity_information' => array('entity_type' => 'some_custom_type', '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); $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->description, '#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->revision, '#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 = language_get_default_configuration('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) { $term = $this->entity; $vocab_storage = $this->entityManager->getStorage('taxonomy_vocabulary'); $vocabulary = $vocab_storage->load($term->bundle()); $parent = array_keys(taxonomy_term_load_parents($term->id())); $form_state['taxonomy']['parent'] = $parent; $form_state['taxonomy']['vocabulary'] = $vocabulary; $language_configuration = $this->moduleHandler->moduleExists('language') ? language_get_default_configuration('taxonomy_term', $vocabulary->id()) : FALSE; $form['langcode'] = array('#type' => 'language_select', '#title' => $this->t('Language'), '#languages' => LanguageInterface::STATE_ALL, '#default_value' => $term->getUntranslated()->language()->id, '#access' => !empty($language_configuration['language_show'])); $form['relations'] = array('#type' => 'details', '#title' => $this->t('Relations'), '#open' => $vocabulary->hierarchy == TAXONOMY_HIERARCHY_MULTIPLE, '#weight' => 10); // taxonomy_get_tree and taxonomy_term_load_parents may contain large // numbers of items so we check for taxonomy.settings:override_selector // before loading the full vocabulary. Contrib modules can then intercept // before hook_form_alter to provide scalable alternatives. if (!$this->config('taxonomy.settings')->get('override_selector')) { $parent = array_keys(taxonomy_term_load_parents($term->id())); $children = taxonomy_get_tree($vocabulary->id(), $term->id()); // A term can't be the child of itself, nor of its children. foreach ($children as $child) { $exclude[] = $child->tid; } $exclude[] = $term->id(); $tree = taxonomy_get_tree($vocabulary->id()); $options = array('<' . $this->t('root') . '>'); if (empty($parent)) { $parent = array(0); } foreach ($tree as $item) { if (!in_array($item->tid, $exclude)) { $options[$item->tid] = str_repeat('-', $item->depth) . $item->name; } } $form['relations']['parent'] = array('#type' => 'select', '#title' => $this->t('Parent terms'), '#options' => $options, '#default_value' => $parent, '#multiple' => TRUE); } $form['relations']['weight'] = array('#type' => 'textfield', '#title' => $this->t('Weight'), '#size' => 6, '#default_value' => $term->getWeight(), '#description' => $this->t('Terms are displayed in ascending order by weight.'), '#required' => TRUE); $form['vid'] = array('#type' => 'value', '#value' => $vocabulary->id()); $form['tid'] = array('#type' => 'value', '#value' => $term->id()); if ($term->isNew()) { $form_state['redirect'] = current_path(); } return parent::form($form, $form_state, $term); }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); $type = $this->entity; if ($this->operation == 'add') { $form['#title'] = String::checkPlain($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->name, '#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' => 'node_type_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->description, '#description' => t('Describe this content type. The text will be displayed on the <em>Add 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->help, '#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 = language_get_default_configuration('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 $form; }
/** * {@inheritdoc} */ public function buildForm(array $form, array &$form_state) { $entity_types = $this->entityManager->getDefinitions(); $labels = array(); $default = array(); $bundles = $this->entityManager->getAllBundleInfo(); $language_configuration = array(); foreach ($entity_types as $entity_type_id => $entity_type) { if (!$entity_type->isTranslatable()) { continue; } $labels[$entity_type_id] = $entity_type->getLabel() ?: $entity_type_id; $default[$entity_type_id] = FALSE; // Check whether we have any custom setting. foreach ($bundles[$entity_type_id] as $bundle => $bundle_info) { $conf = language_get_default_configuration($entity_type_id, $bundle); if (!empty($conf['language_show']) || $conf['langcode'] != 'site_default') { $default[$entity_type_id] = $entity_type_id; } $language_configuration[$entity_type_id][$bundle] = $conf; } } asort($labels); $form = array('#labels' => $labels, '#attached' => array('library' => array('language/drupal.language.admin'))); $form['entity_types'] = array('#title' => $this->t('Custom language settings'), '#type' => 'checkboxes', '#options' => $labels, '#default_value' => $default); $form['settings'] = array('#tree' => TRUE); foreach ($labels as $entity_type_id => $label) { $entity_type = $entity_types[$entity_type_id]; $form['settings'][$entity_type_id] = array('#title' => $label, '#type' => 'container', '#entity_type' => $entity_type_id, '#theme' => 'language_content_settings_table', '#bundle_label' => $entity_type->getBundleLabel() ?: $label, '#states' => array('visible' => array(':input[name="entity_types[' . $entity_type_id . ']"]' => array('checked' => TRUE)))); foreach ($bundles[$entity_type_id] as $bundle => $bundle_info) { $form['settings'][$entity_type_id][$bundle]['settings'] = array('#type' => 'item', '#label' => $bundle_info['label'], 'language' => array('#type' => 'language_configuration', '#entity_information' => array('entity_type' => $entity_type_id, 'bundle' => $bundle), '#default_value' => $language_configuration[$entity_type_id][$bundle])); } } $form = parent::buildForm($form, $form_state); // @todo Remove this override. There are tests that check for explicitly for // the button label which need to be adapted for that. // https://drupal.org/node/2241727 $form['actions']['submit']['#value'] = $this->t('Save'); return $form; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $entity_types = $this->entityManager->getDefinitions(); $labels = array(); $default = array(); $bundles = $this->entityManager->getAllBundleInfo(); $language_configuration = array(); foreach ($entity_types as $entity_type_id => $entity_type) { if (!$entity_type->isTranslatable()) { continue; } $labels[$entity_type_id] = $entity_type->getLabel() ?: $entity_type_id; $default[$entity_type_id] = FALSE; // Check whether we have any custom setting. foreach ($bundles[$entity_type_id] as $bundle => $bundle_info) { $conf = language_get_default_configuration($entity_type_id, $bundle); if (!empty($conf['language_show']) || $conf['langcode'] != LanguageInterface::LANGCODE_SITE_DEFAULT) { $default[$entity_type_id] = $entity_type_id; } $language_configuration[$entity_type_id][$bundle] = $conf; } } asort($labels); $form = array('#labels' => $labels, '#attached' => array('library' => array('language/drupal.language.admin'))); $form['entity_types'] = array('#title' => $this->t('Custom language settings'), '#type' => 'checkboxes', '#options' => $labels, '#default_value' => $default); $form['settings'] = array('#tree' => TRUE); foreach ($labels as $entity_type_id => $label) { $entity_type = $entity_types[$entity_type_id]; $form['settings'][$entity_type_id] = array('#title' => $label, '#type' => 'container', '#entity_type' => $entity_type_id, '#theme' => 'language_content_settings_table', '#bundle_label' => $entity_type->getBundleLabel() ?: $label, '#states' => array('visible' => array(':input[name="entity_types[' . $entity_type_id . ']"]' => array('checked' => TRUE)))); foreach ($bundles[$entity_type_id] as $bundle => $bundle_info) { $form['settings'][$entity_type_id][$bundle]['settings'] = array('#type' => 'item', '#label' => $bundle_info['label'], 'language' => array('#type' => 'language_configuration', '#entity_information' => array('entity_type' => $entity_type_id, 'bundle' => $bundle), '#default_value' => $language_configuration[$entity_type_id][$bundle])); } } $form = parent::buildForm($form, $form_state); return $form; }
/** * {@inheritdoc} */ public function form(array $form, array &$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')); $options = array(); foreach ($this->entityManager->getDefinitions() as $entity_type) { if ($entity_type->isFieldable()) { $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); if ($this->moduleHandler->moduleExists('content_translation')) { $form['language'] = array('#type' => 'details', '#title' => t('Language settings'), '#group' => 'additional_settings'); $language_configuration = language_get_default_configuration('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; }
/** * Overrides EntityForm::form(). */ public function form(array $form, array &$form_state) { $menu_link = $this->entity; // Since menu_link_load() no longer returns a translated and access checked // item, do it here instead. _menu_link_translate($menu_link); $form['link_title'] = array('#type' => 'textfield', '#title' => t('Menu link title'), '#default_value' => $menu_link->link_title, '#description' => t('The text to be used for this link in the menu.'), '#required' => TRUE); foreach (array('link_path', 'mlid', 'module', 'has_children', 'options') as $key) { $form[$key] = array('#type' => 'value', '#value' => $menu_link->{$key}); } // Any item created or edited via this interface is considered "customized". $form['customized'] = array('#type' => 'value', '#value' => 1); // We are not using url() when constructing this path because it would add // $base_path. $path = $menu_link->link_path; if (isset($menu_link->options['query'])) { $path .= '?' . $this->urlGenerator->httpBuildQuery($menu_link->options['query']); } if (isset($menu_link->options['fragment'])) { $path .= '#' . $menu_link->options['fragment']; } if ($menu_link->module == 'menu_ui') { $form['link_path'] = array('#type' => 'textfield', '#title' => t('Path'), '#maxlength' => 255, '#default_value' => $path, '#description' => t('The path for this menu link. This can be an internal Drupal path such as %add-node or an external URL such as %drupal. Enter %front to link to the front page.', array('%front' => '<front>', '%add-node' => 'node/add', '%drupal' => 'http://drupal.org')), '#required' => TRUE); } else { $form['_path'] = array('#type' => 'item', '#title' => t('Path'), '#description' => l($menu_link->link_title, $menu_link->href, $menu_link->options)); } $form['description'] = array('#type' => 'textarea', '#title' => t('Description'), '#default_value' => isset($menu_link->options['attributes']['title']) ? $menu_link->options['attributes']['title'] : '', '#rows' => 1, '#description' => t('Shown when hovering over the menu link.')); $form['enabled'] = array('#type' => 'checkbox', '#title' => t('Enabled'), '#default_value' => !$menu_link->hidden, '#description' => t('Menu links that are not enabled will not be listed in any menu.')); $form['expanded'] = array('#type' => 'checkbox', '#title' => t('Show as expanded'), '#default_value' => $menu_link->expanded, '#description' => t('If selected and this menu link has children, the menu will always appear expanded.')); // Generate a list of possible parents (not including this link or descendants). $options = menu_ui_parent_options(menu_ui_get_menus(), $menu_link); $default = $menu_link->menu_name . ':' . $menu_link->plid; if (!isset($options[$default])) { $default = 'tools:0'; } $form['parent'] = array('#type' => 'select', '#title' => t('Parent link'), '#default_value' => $default, '#options' => $options, '#description' => t('The maximum depth for a link and all its children is fixed at !maxdepth. Some menu links may not be available as parents if selecting them would exceed this limit.', array('!maxdepth' => MENU_MAX_DEPTH)), '#attributes' => array('class' => array('menu-title-select'))); // Get number of items in menu so the weight selector is sized appropriately. $delta = $this->menuLinkStorage->countMenuLinks($menu_link->menu_name); $form['weight'] = array('#type' => 'weight', '#title' => t('Weight'), '#delta' => max($delta, 50), '#default_value' => $menu_link->weight, '#description' => t('Optional. In the menu, the heavier links will sink and the lighter links will be positioned nearer the top.')); // Language module allows to configure the menu link language independently // of the menu language. It also allows to optionally show the language // selector on the menu link form so that the language of each menu link can // be configured individually. if ($this->moduleHandler->moduleExists('language')) { $language_configuration = language_get_default_configuration('menu_link', $menu_link->bundle()); $default_langcode = $menu_link->isNew() ? $language_configuration['langcode'] : $menu_link->langcode; $language_show = $language_configuration['language_show']; } else { $default_langcode = $menu_link->isNew() ? entity_load('menu', $menu_link->menu_name)->language()->getId() : $menu_link->langcode; $language_show = FALSE; } $form['langcode'] = array('#type' => 'language_select', '#title' => t('Language'), '#languages' => LanguageInterface::STATE_ALL, '#default_value' => $default_langcode, '#access' => $language_show); return parent::form($form, $form_state, $menu_link); }
/** * 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_show]' => 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_show]' => 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.'); $settings = content_translation_get_config('comment', 'comment_article', 'fields'); $this->assertFalse(isset($settings['comment_body']), 'Configurable fields are not saved to content_translation.settings.'); $this->assertTrue(isset($settings['subject']), 'Base fields are saved to content_translation.settings.'); // 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); $settings = content_translation_get_config('entity_test_mul', 'entity_test_mul', 'fields'); $this->assertTrue($settings['name'] && !$settings['user_id'], 'Base fields are saved to content_translation.settings.'); $definitions = $this->entityManager()->getFieldDefinitions('entity_test_mul', 'entity_test_mul'); $this->assertTrue($definitions['name']->isTranslatable() && !$definitions['user_id']->isTranslatable(), 'Bundle field definitions were correctly altered.'); // Test that language settings are correctly stored. $language_configuration = language_get_default_configuration('comment', 'comment_article'); $this->assertEqual($language_configuration['langcode'], 'current_interface', 'The default language for article comments is set to the current interface language.'); $this->assertTrue($language_configuration['language_show'], '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_show]' => 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); $instance = FieldInstanceConfig::loadByName('node', 'article', 'body'); $definitions = \Drupal::entityManager()->getFieldDefinitions('node', 'article'); $this->assertEqual($definitions['body']->isTranslatable(), $translatable, 'Field translatability correctly switched.'); $this->assertEqual($instance->isTranslatable(), $definitions['body']->isTranslatable(), 'Configurable field translatability correctly switched.'); // Test that also the Field UI form behaves correctly. $translatable = !$translatable; $edit = array('instance[translatable]' => $translatable); $this->drupalPostForm('admin/structure/types/manage/article/fields/node.article.body', $edit, t('Save settings')); \Drupal::entityManager()->clearCachedFieldDefinitions(); $instance = FieldInstanceConfig::loadByName('node', 'article', 'body'); $definitions = \Drupal::entityManager()->getFieldDefinitions('node', 'article'); $this->assertEqual($definitions['body']->isTranslatable(), $translatable, 'Field translatability correctly switched.'); $this->assertEqual($instance->isTranslatable(), $definitions['body']->isTranslatable(), 'Configurable field translatability correctly switched.'); } }
/** * {@inheritdoc} */ public function form(array $form, array &$form_state) { $block = $this->entity; $account = $this->currentUser(); if ($this->operation == 'edit') { $form['#title'] = $this->t('Edit custom block %label', array('%label' => $block->label())); } // Override the default CSS class name, since the user-defined custom block // type name in 'TYPE-block-form' potentially clashes with third-party class // names. $form['#attributes']['class'][0] = drupal_html_class('block-' . $block->bundle() . '-form'); if ($this->moduleHandler->moduleExists('language')) { $language_configuration = language_get_default_configuration('block_content', $block->bundle()); // Set the correct default language. if ($block->isNew()) { $language_default = $this->languageManager->getCurrentLanguage($language_configuration['langcode']); $block->langcode->value = $language_default->id; } } $form['langcode'] = array('#title' => $this->t('Language'), '#type' => 'language_select', '#default_value' => $block->getUntranslated()->language()->id, '#languages' => LanguageInterface::STATE_ALL, '#access' => isset($language_configuration['language_show']) && $language_configuration['language_show']); $form['advanced'] = array('#type' => 'vertical_tabs', '#weight' => 99); // Add a log field if the "Create new revision" option is checked, or if the // current user has the ability to check that option. $form['revision_information'] = array('#type' => 'details', '#title' => $this->t('Revision information'), '#open' => $block->isNewRevision(), '#group' => 'advanced', '#attributes' => array('class' => array('block-content-form-revision-information')), '#attached' => array('library' => array('block_content/drupal.block_content')), '#weight' => 20, '#access' => $block->isNewRevision() || $account->hasPermission('administer blocks')); $form['revision_information']['revision'] = array('#type' => 'checkbox', '#title' => $this->t('Create new revision'), '#default_value' => $block->isNewRevision(), '#access' => $account->hasPermission('administer blocks')); // Check the revision log checkbox when the log textarea is filled in. // This must not happen if "Create new revision" is enabled by default, // since the state would auto-disable the checkbox otherwise. if (!$block->isNewRevision()) { $form['revision_information']['revision']['#states'] = array('checked' => array('textarea[name="revision_log"]' => array('empty' => FALSE))); } $form['revision_information']['revision_log'] = array('#type' => 'textarea', '#title' => $this->t('Revision log message'), '#rows' => 4, '#default_value' => $block->getRevisionLog(), '#description' => $this->t('Briefly describe the changes you have made.')); return parent::form($form, $form_state, $block); }
/** * 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' => drupal_strtolower($this->randomMachineName()), 'default_language[langcode]' => 'bb', 'default_language[language_show]' => 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 = language_get_default_configuration('taxonomy_term', $edit['vid']); $this->assertEqual($language_settings['langcode'], 'bb', 'The langcode was saved.'); $this->assertTrue($language_settings['language_show'], '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-show', '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_show]' => FALSE); $this->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save')); // And check again the settings and also the interface. $language_settings = language_get_default_configuration('taxonomy_term', $vid); $this->assertEqual($language_settings['langcode'], 'aa', 'The langcode was saved.'); $this->assertFalse($language_settings['language_show'], '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-show', '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_show]' => FALSE); $this->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save')); // Check that we have the new settings. $new_settings = language_get_default_configuration('taxonomy_term', $vid); $this->assertEqual($new_settings['langcode'], 'authors_default', 'The langcode was saved.'); $this->assertFalse($new_settings['language_show'], 'The new visibility setting was saved.'); }
/** * Tests that the configuration is updated when the node type is changed. */ public function testNodeTypeUpdate() { // Create the article content type first if the profile used is not the // standard one. if ($this->profile != 'standard') { $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article')); } $admin_user = $this->drupalCreateUser(array('administer content types')); $this->drupalLogin($admin_user); $edit = array('language_configuration[langcode]' => 'current_interface', 'language_configuration[language_show]' => TRUE); $this->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type')); // Check the language default configuration for the articles. $configuration = language_get_default_configuration('node', 'article'); $this->assertEqual($configuration, array('langcode' => 'current_interface', 'language_show' => TRUE), 'The default language configuration has been saved on the Article content type.'); // Rename the article content type. $edit = array('type' => 'article_2'); $this->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type')); // Check that we still have the settings for the new node type. $configuration = language_get_default_configuration('node', 'article_2'); $this->assertEqual($configuration, array('langcode' => 'current_interface', 'language_show' => TRUE), 'The default language configuration has been kept on the new Article content type.'); }
/** * 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 (node_type_get_types() 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 = language_get_default_configuration('node', $bundle->type); if ($configuration['language_show']) { $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 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->randomName(16)); $label = $this->randomString(); $edit = array('id' => $menu_name, 'description' => '', 'label' => $label, 'langcode' => 'aa', 'default_language[langcode]' => 'bb', 'default_language[language_show]' => TRUE); $this->drupalPostForm('admin/structure/menu/add', $edit, t('Save')); // Check that the language settings were saved. $this->assertEqual(entity_load('menu', $menu_name)->language()->getId(), $edit['langcode']); $language_settings = language_get_default_configuration('menu_link', $menu_name); $this->assertEqual($language_settings['langcode'], 'bb'); $this->assertEqual($language_settings['language_show'], TRUE); // Check menu language and item language configuration. $this->assertOptionSelected('edit-langcode', $edit['langcode'], 'The menu language was correctly selected.'); $this->assertOptionSelected('edit-default-language-langcode', $edit['default_language[langcode]'], 'The menu link default language was correctly selected.'); $this->assertFieldChecked('edit-default-language-language-show'); // Test menu link language. $link_path = '<front>'; // Add a menu link. $link_title = $this->randomString(); $edit = array('link_title' => $link_title, 'link_path' => $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', array('link_title' => $link_title)); $menu_link = reset($menu_links); $this->assertMenuLink($menu_link->id(), array('menu_name' => $menu_name, 'link_path' => $link_path, 'langcode' => 'bb')); // Edit menu link default, changing it to cc. $edit = array('default_language[langcode]' => 'cc'); $this->drupalPostForm("admin/structure/menu/manage/{$menu_name}", $edit, t('Save')); // Check cc is the menu link default. $this->assertOptionSelected('edit-default-language-langcode', $edit['default_language[langcode]'], 'The menu link default language was correctly selected.'); // Add a menu link. $link_title = $this->randomString(); $edit = array('link_title' => $link_title, 'link_path' => $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', array('link_title' => $link_title)); $menu_link = reset($menu_links); $this->assertMenuLink($menu_link->id(), array('menu_name' => $menu_name, 'link_path' => $link_path, 'langcode' => 'cc')); // Now change the language of the new link to 'bb'. $edit = array('langcode' => 'bb'); $this->drupalPostForm('admin/structure/menu/item/' . $menu_link->id() . '/edit', $edit, t('Save')); $this->assertMenuLink($menu_link->id(), array('menu_name' => $menu_name, 'link_path' => $link_path, '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', 'bb', 'The menu link language was correctly selected.'); // Edit menu to hide the language select on menu link item add. $edit = array('default_language[language_show]' => FALSE); $this->drupalPostForm("admin/structure/menu/manage/{$menu_name}", $edit, t('Save')); $this->assertNoFieldChecked('edit-default-language-language-show'); // Check that the language settings were saved. $language_settings = language_get_default_configuration('menu_link', $menu_name); $this->assertEqual($language_settings['langcode'], 'cc'); $this->assertEqual($language_settings['language_show'], FALSE); // 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', 'The language selector field was hidden the page'); }