/** * Create a new group. * @param array $data * Data for the field group. */ function createGroup($entity_type, $bundle, $context, $mode, array $data) { if (!isset($data['format_settings'])) { $data['format_settings'] = array(); } $data['format_settings'] += _field_group_get_default_formatter_settings($data['format_type']); $group_name = 'group_' . Unicode::strtolower($this->randomMachineName()); $field_group = (object) array('group_name' => $group_name, 'entity_type' => $entity_type, 'bundle' => $bundle, 'mode' => $mode, 'context' => $context, 'children' => isset($data['children']) ? $data['children'] : array(), 'parent_name' => isset($data['parent']) ? $data['parent'] : '', 'weight' => isset($data['weight']) ? $data['weight'] : 0, 'label' => isset($data['label']) ? $data['label'] : $this->randomString(8), 'format_type' => $data['format_type'], 'format_settings' => $data['format_settings']); field_group_group_save($field_group); return $field_group; }
/** * Create field group instance using internal definition array. * * @return object * Field group configuration object. */ public function save() { $service = new Config(); $group = $service->loadFieldGroupByIdentifier($this->definition->identifier); if ($group) { foreach ($this->definition as $name => $value) { $group->{$name} = $this->definition->{$name}; } field_group_group_save($group); return $group; } else { return field_group_group_save($this->definition); } }
/** * Test the tabs formatter. */ function testTabs() { $data = array('label' => 'Tab 1', 'weight' => '1', 'children' => array(0 => 'field_test'), 'format_type' => 'tab', 'format_settings' => array('label' => 'Tab 1', 'classes' => 'test-class', 'description' => '', 'formatter' => 'open')); $first_tab = $this->createGroup('node', $this->type, 'view', 'default', $data); $data = array('label' => 'Tab 2', 'weight' => '1', 'children' => array(0 => 'field_test_2'), 'format_type' => 'tab', 'format_settings' => array('label' => 'Tab 1', 'classes' => 'test-class-2', 'description' => 'description of second tab', 'formatter' => 'closed')); $second_tab = $this->createGroup('node', $this->type, 'view', 'default', $data); $data = array('label' => 'Tabs', 'weight' => '1', 'children' => array(0 => $first_tab->group_name, 1 => $second_tab->group_name), 'format_type' => 'tabs', 'format_settings' => array('direction' => 'vertical', 'label' => 'Tab 1', 'classes' => 'test-class-wrapper')); $tabs_group = $this->createGroup('node', $this->type, 'view', 'default', $data); $this->drupalGet('node/' . $this->node->id()); // Test properties. $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]", NULL, t('Test class set on tabs wrapper')); $this->assertFieldByXPath("//details[contains(@class, 'test-class-2')]", NULL, t('Test class set on second tab')); $this->assertRaw('<div class="details-description">description of second tab</div>', t('Description of tab is shown')); $this->assertRaw('class="collapsible collapsed test-class-2', t('Second tab is default collapsed')); // Test if correctly nested. $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]//details[contains(@class, 'test-class')]", NULL, 'First tab is displayed as child of the wrapper.'); $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]//details[contains(@class, 'test-class-2')]", NULL, 'Second tab is displayed as child of the wrapper.'); // Test if it's a vertical tab. $this->assertFieldByXPath('//div[@data-vertical-tabs-panes=""]', NULL, 'Tabs are shown vertical.'); // Switch to horizontal $tabs_group->format_settings['direction'] = 'horizontal'; field_group_group_save($tabs_group); $this->drupalGet('node/' . $this->node->id()); // Test if it's a horizontal tab. $this->assertFieldByXPath('//div[@data-horizontal-tabs-panes=""]', NULL, 'Tabs are shown horizontal.'); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { if ($form_state->get('step') == 'formatter') { $form_state->set('step', 'configuration'); $form_state->set('group_label', $form_state->getValue('label')); $form_state->set('group_name', $form_state->getValue('group_name')); $form_state->set('group_formatter', $form_state->getValue('group_formatter')); $form_state->setRebuild(); } else { $new_group = (object) array('group_name' => $form_state->get('group_name'), 'entity_type' => $this->entityTypeId, 'bundle' => $this->bundle, 'mode' => $this->mode, 'context' => $this->context, 'children' => [], 'parent_name' => '', 'weight' => 20, 'label' => $form_state->get('group_label'), 'format_type' => $form_state->get('group_formatter')); $new_group->format_settings = $form_state->getValue('format_settings'); $new_group->format_settings += _field_group_get_default_formatter_settings($form_state->get('group_formatter'), $this->context); field_group_group_save($new_group); // Store new group information for any additional submit handlers. $groups_added = $form_state->get('groups_added'); $groups_added['_add_new_group'] = $new_group->group_name; drupal_set_message(t('New group %label successfully created.', array('%label' => $new_group->label))); $form_state->setRedirectUrl(FieldgroupUi::getFieldUiRoute($new_group)); \Drupal::cache()->invalidate('field_groups'); } }
/** * Create a field in a group and in a vocabulary. * * @param string $field_name * Name of the field. * @param string $field_type * Type of the field. * @param string $group_name * Name of the group. * @param string $vocabulary_name * Name of the vocabulary. * * @Given the field :field_type named :field_name grouped in :group_name in the vocabulary :vocabulary_name exists * * @Then I create a new field :field_type named :field_name grouped in :group_name in the vocabulary :vocabulary_name */ public function iCreateNewFieldNamedGroupedInInTheVocabulary($field_name, $field_type, $group_name, $vocabulary_name) { $field_machine_name = $this->transliterate->getMachineName('field_' . $field_name); $field_machine_type = $this->getFieldTypeFormatByName($field_type); $group_machine_name = $this->transliterate->getMachineName('group_' . $group_name); $vocabulary_machine_name = $this->transliterate->getMachineName($vocabulary_name); // Make sure the field doesn't already exist. if (!field_info_field($field_machine_name)) { // Create a field. $field = array('field_name' => $field_machine_name, 'type' => $field_machine_type, 'label' => $field_name); field_create_field($field); // Attach the field to our taxonomy entity. $instance = array('field_name' => $field_machine_name, 'entity_type' => 'taxonomy_term', 'bundle' => $vocabulary_machine_name, 'label' => $field_name, 'description' => ''); field_create_instance($instance); // Backup fields created. $this->fields[] = $field; $groups = field_group_read_groups(array('name' => 'taxonomy_term', 'bundle' => $vocabulary_machine_name, 'view_mode' => 'full')); $your_group = $groups['taxonomy_term'][$vocabulary_machine_name]['form'][$group_machine_name]; $your_group->children[] = $field_machine_name; field_group_group_save($your_group); } else { throw new \InvalidArgumentException("The field '{$field_name}' already exists."); } }