/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, $plugin = NULL, $item_type = NULL) { // Look for the first source overview local task and make the parent use the // same route arguments. // @todo: Find a nicer way to get the main source (usually entity:node), also, // this looses the trail because the parent points directly to the first // sub-task. Use a different route that doesn't require the arguments? if (!isset($plugin)) { $definitions = $this->sourceManager->getDefinitions(); if (empty($definitions)) { return array('#markup' => 'No sources enabled.'); } if (isset($definitions['content'])) { $plugin = 'content'; } else { $plugin = key($definitions); } } $source = $this->sourceManager->createInstance($plugin); if (!isset($item_type)) { $item_types = $source->getItemTypes(); if (empty($item_types)) { return array('#markup' => 'No sources enabled.'); } if (isset($item_types['node'])) { $item_type = 'node'; } else { $item_type = key($item_types); } } $definition = $this->sourceManager->getDefinition($plugin); $form['#title'] = $this->t('@type overview (@plugin)', array('@type' => $source->getItemTypeLabel($item_type), '@plugin' => $definition['label'])); $options = array(); foreach ($this->sourceManager->getDefinitions() as $type => $definition) { $plugin_type = $this->sourceManager->createInstance($type); $item_types = $plugin_type->getItemTypes(); asort($item_types); foreach ($item_types as $item_types => $item_label) { $options[(string) $definition['label']][$type . ':' . $item_types] = $item_label; } } $form['source_type'] = array('#type' => 'container', '#open' => TRUE, '#attributes' => array('class' => array('tmgmt-source-operations-wrapper')), '#weight' => -100); $form['source_type']['source'] = array('#type' => 'select', '#options' => $options, '#default_value' => $plugin . ':' . $item_type, '#title' => t('Choose source'), '#ajax' => array('callback' => array($this, 'ajaxCallback'))); $form['source_type']['choose'] = array('#type' => 'submit', '#value' => t('Choose'), '#submit' => array('::sourceSelectSubmit'), '#attributes' => array('class' => array('js-hide'))); $form['actions'] = array('#type' => 'details', '#title' => t('Operations'), '#open' => TRUE, '#attributes' => array('class' => array('tmgmt-source-operations-wrapper'))); $form['actions']['submit'] = array('#type' => 'submit', '#button_type' => 'primary', '#validate' => array('::validateItemsSelected'), '#value' => t('Request translation'), '#submit' => array('::submitForm')); tmgmt_add_cart_form($form['actions'], $form_state, $plugin, $item_type); if ($source instanceof ContinuousSourceInterface && $this->continuousManager->hasContinuousJobs()) { $form['actions']['add_to_continuous_jobs'] = array('#type' => 'submit', '#validate' => array('::validateItemsSelected'), '#value' => t('Check for continuous jobs'), '#submit' => array('::submitToContinuousJobs')); $form['actions']['add_all_to_continuous_jobs'] = array('#type' => 'checkbox', '#title' => 'All (continuous check only)', '#default_value' => FALSE); } $source_ui = $this->sourceManager->createUIInstance($plugin); $form_state->set('plugin', $plugin); $form_state->set('item_type', $item_type); $form = $source_ui->overviewForm($form, $form_state, $item_type); $form['legend'] = tmgmt_color_legend(); return $form; }
/** * {@inheritdoc} */ function buildForm(array $form, FormStateInterface $form_state, array $build = NULL) { // Store the entity in the form state so we can easily create the job in the // submit handler. $form_state->set('entity', $build['#entity']); $overview = $build['content_translation_overview']; $form['#title'] = $this->t('Translations of @title', array('@title' => $build['#entity']->label())); $form['actions'] = array('#type' => 'details', '#title' => t('Operations'), '#open' => TRUE, '#attributes' => array('class' => array('tmgmt-source-operations-wrapper'))); $form['actions']['request'] = array('#type' => 'submit', '#button_type' => 'primary', '#value' => $this->t('Request translation'), '#submit' => array('::submitForm')); tmgmt_add_cart_form($form['actions'], $form_state, 'content', $form_state->get('entity')->getEntityTypeId(), $form_state->get('entity')->id()); // Inject our additional column into the header. array_splice($overview['#header'], -1, 0, array(t('Pending Translations'))); // Make this a tableselect form. $form['languages'] = array('#type' => 'tableselect', '#header' => $overview['#header'], '#options' => array()); $languages = \Drupal::languageManager()->getLanguages(); // Check if there is a job / job item that references this translation. $entity_langcode = $form_state->get('entity')->language()->getId(); $items = tmgmt_job_item_load_latest('content', $form_state->get('entity')->getEntityTypeId(), $form_state->get('entity')->id(), $entity_langcode); foreach ($languages as $langcode => $language) { if ($langcode == LanguageInterface::LANGCODE_DEFAULT) { // Never show language neutral on the overview. continue; } // Since the keys are numeric and in the same order we can shift one element // after the other from the original non-form rows. $option = array_shift($overview['#rows']); if ($langcode == $entity_langcode) { $additional = array('data' => array('#markup' => '<strong>' . t('Source') . '</strong>')); // This is the source object so we disable the checkbox for this row. $form['languages'][$langcode] = array('#type' => 'checkbox', '#disabled' => TRUE); } elseif (isset($items[$langcode])) { $item = $items[$langcode]; $states = JobItem::getStates(); $path = \Drupal::routeMatch()->getRouteName() ? Url::fromRouteMatch(\Drupal::routeMatch())->getInternalPath() : ''; $destination = array('destination' => $path); $additional = \Drupal::l($states[$item->getState()], $item->urlInfo()->setOption('query', $destination)); // Disable the checkbox for this row since there is already a translation // in progress that has not yet been finished. This way we make sure that // we don't stack multiple active translations for the same item on top // of each other. $form['languages'][$langcode] = array('#type' => 'checkbox', '#disabled' => TRUE); } else { // There is no translation job / job item for this target language. $additional = t('None'); } // Inject the additional column into the array. // The generated form structure has changed, support both an additional // 'data' key (that is not supported by tableselect) and the old version // without. if (isset($option['data'])) { array_splice($option['data'], -1, 0, array($additional)); // Append the current option array to the form. $form['languages']['#options'][$langcode] = $option['data']; } else { array_splice($option, -1, 0, array($additional)); // Append the current option array to the form. $form['languages']['#options'][$langcode] = $option; } } return $form; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, array $build = NULL, $plugin_id = NULL) { // Store the entity in the form state so we can easily create the job in the // submit handler. $mapper_definition = \Drupal::service('plugin.manager.config_translation.mapper')->getDefinition($plugin_id); /** @var \Drupal\config_translation\ConfigMapperInterface $mapper */ $mapper = $this->configMapperManager->createInstance($plugin_id); $mapper->populateFromRouteMatch($this->routeMatch); $form_state->set('mapper', $mapper); if (!isset($mapper_definition['entity_type'])) { $form_state->set('item_type', ConfigSource::SIMPLE_CONFIG); $form_state->set('item_id', $mapper_definition['id']); } else { $id = $mapper->getConfigNames()[0]; $form_state->set('id', $id); $form_state->set('item_type', $plugin_id); $form_state->set('item_id', $id); } $form['#title'] = $this->t('Translations of @title', array('@title' => $mapper->getTitle())); $overview = $build['languages']; $form['top_actions'] = array('#type' => 'details', '#title' => t('Operations'), '#open' => TRUE, '#attributes' => array('class' => array('tmgmt-source-operations-wrapper'))); $form['top_actions']['request'] = array('#type' => 'submit', '#button_type' => 'primary', '#value' => $this->t('Request translation'), '#submit' => array('::submitForm')); tmgmt_add_cart_form($form['top_actions'], $form_state, 'config', $form_state->get('item_type'), $form_state->get('item_id')); // Inject our additional column into the header. array_splice($overview['#header'], -1, 0, array(t('Pending Translations'))); // Make this a tableselect form. $form['languages'] = array('#type' => 'tableselect', '#header' => $overview['#header'], '#options' => array()); $languages = \Drupal::languageManager()->getLanguages(); // Check if there is a job / job item that references this translation. $items = tmgmt_job_item_load_latest('config', $form_state->get('item_type'), $form_state->get('item_id'), $mapper->getLangcode()); foreach ($languages as $langcode => $language) { if ($langcode == LanguageInterface::LANGCODE_DEFAULT) { // Never show language neutral on the overview. continue; } // Since the keys are numeric and in the same order we can shift one element // after the other from the original non-form rows. $option = $overview[$langcode]; if ($langcode == $mapper->getLangcode()) { $additional = array('data' => array('#markup' => '<strong>' . t('Source') . '</strong>')); // This is the source object so we disable the checkbox for this row. $form['languages'][$langcode] = array('#type' => 'checkbox', '#disabled' => TRUE); } elseif (isset($items[$langcode])) { $item = $items[$langcode]; $states = JobItem::getStates(); $additional = \Drupal::l($states[$item->getState()], $item->urlInfo()->setOption('query', array('destination' => Url::fromRoute('<current>')->getInternalPath()))); // Disable the checkbox for this row since there is already a translation // in progress that has not yet been finished. This way we make sure that // we don't stack multiple active translations for the same item on top // of each other. $form['languages'][$langcode] = array('#type' => 'checkbox', '#disabled' => TRUE); } else { // There is no translation job / job item for this target language. $additional = t('None'); } // Inject the additional column into the array. // The generated form structure has changed, support both an additional // 'data' key (that is not supported by tableselect) and the old version // without. if (isset($option['data'])) { array_splice($option['data'], -1, 0, array($additional)); // Append the current option array to the form. $form['languages']['#options'][$langcode] = $option['data']; } else { array_splice($option, -1, 0, array($additional)); // Append the current option array to the form. $form['languages']['#options'][$langcode] = array(drupal_render($option['language']), $additional, drupal_render($option['operations'])); } } return $form; }