/** * Alters the field to render a link. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity being rendered. * @param \Drupal\views\ResultRow $values * The current row of the views result. * * @return string * The actual rendered text (without the link) of this field. */ protected function renderLink(EntityInterface $entity, ResultRow $values) { if (content_translation_translate_access($entity)->isAllowed()) { $text = !empty($this->options['text']) ? $this->options['text'] : $this->t('Translate'); $this->options['alter']['make_link'] = TRUE; $this->options['alter']['path'] = $entity->getSystemPath('drupal:content-translation-overview'); return $text; } }
/** * Wraps content_translation_translate_access. */ protected function access(EntityInterface $entity) { return content_translation_translate_access($entity); }
/** * Form constructor. * * Display a tree of all the terms in a vocabulary, with options to edit * each one. The form is made drag and drop by the theme function. * * @param array $form * An associative array containing the structure of the form. * @param array $form_state * An associative array containing the current state of the form. * @param \Drupal\taxonomy\VocabularyInterface $taxonomy_vocabulary * The vocabulary to display the overview form for. * * @return array * The form structure. */ public function buildForm(array $form, array &$form_state, VocabularyInterface $taxonomy_vocabulary = NULL) { // @todo Remove global variables when http://drupal.org/node/2044435 is in. global $pager_page_array, $pager_total, $pager_total_items; $form_state['taxonomy']['vocabulary'] = $taxonomy_vocabulary; $parent_fields = FALSE; $page = $this->getRequest()->query->get('page') ?: 0; // Number of terms per page. $page_increment = $this->config('taxonomy.settings')->get('terms_per_page_admin'); // Elements shown on this page. $page_entries = 0; // Elements at the root level before this page. $before_entries = 0; // Elements at the root level after this page. $after_entries = 0; // Elements at the root level on this page. $root_entries = 0; // Terms from previous and next pages are shown if the term tree would have // been cut in the middle. Keep track of how many extra terms we show on // each page of terms. $back_step = NULL; $forward_step = 0; // An array of the terms to be displayed on this page. $current_page = array(); $delta = 0; $term_deltas = array(); // @todo taxonomy_get_tree needs to be converted to a service and injected. // Will be fixed in http://drupal.org/node/1976298. $tree = taxonomy_get_tree($taxonomy_vocabulary->id(), 0, NULL, TRUE); $term = current($tree); do { // In case this tree is completely empty. if (empty($term)) { break; } $delta++; // Count entries before the current page. if ($page && $page * $page_increment > $before_entries && !isset($back_step)) { $before_entries++; continue; } elseif ($page_entries > $page_increment && isset($complete_tree)) { $after_entries++; continue; } // Do not let a term start the page that is not at the root. if (isset($term->depth) && $term->depth > 0 && !isset($back_step)) { $back_step = 0; while ($pterm = prev($tree)) { $before_entries--; $back_step++; if ($pterm->depth == 0) { prev($tree); // Jump back to the start of the root level parent. continue 2; } } } $back_step = isset($back_step) ? $back_step : 0; // Continue rendering the tree until we reach the a new root item. if ($page_entries >= $page_increment + $back_step + 1 && $term->depth == 0 && $root_entries > 1) { $complete_tree = TRUE; // This new item at the root level is the first item on the next page. $after_entries++; continue; } if ($page_entries >= $page_increment + $back_step) { $forward_step++; } // Finally, if we've gotten down this far, we're rendering a term on this // page. $page_entries++; $term_deltas[$term->id()] = isset($term_deltas[$term->id()]) ? $term_deltas[$term->id()] + 1 : 0; $key = 'tid:' . $term->id() . ':' . $term_deltas[$term->id()]; // Keep track of the first term displayed on this page. if ($page_entries == 1) { $form['#first_tid'] = $term->id(); } // Keep a variable to make sure at least 2 root elements are displayed. if ($term->parents[0] == 0) { $root_entries++; } $current_page[$key] = $term; } while ($term = next($tree)); // Because we didn't use a pager query, set the necessary pager variables. $total_entries = $before_entries + $page_entries + $after_entries; $pager_total_items[0] = $total_entries; $pager_page_array[0] = $page; $pager_total[0] = ceil($total_entries / $page_increment); // If this form was already submitted once, it's probably hit a validation // error. Ensure the form is rebuilt in the same order as the user // submitted. if (!empty($form_state['input'])) { // Get the POST order. $order = array_flip(array_keys($form_state['input']['terms'])); // Update our form with the new order. $current_page = array_merge($order, $current_page); foreach ($current_page as $key => $term) { // Verify this is a term for the current page and set at the current // depth. if (is_array($form_state['input']['terms'][$key]) && is_numeric($form_state['input']['terms'][$key]['term']['tid'])) { $current_page[$key]->depth = $form_state['input']['terms'][$key]['term']['depth']; } else { unset($current_page[$key]); } } } $errors = form_get_errors($form_state); $destination = drupal_get_destination(); $row_position = 0; // Build the actual form. $form['terms'] = array('#type' => 'table', '#header' => array($this->t('Name'), $this->t('Weight'), $this->t('Operations')), '#empty' => $this->t('No terms available. <a href="@link">Add term</a>.', array('@link' => url('admin/structure/taxonomy/manage/' . $taxonomy_vocabulary->id() . '/add'))), '#attributes' => array('id' => 'taxonomy')); foreach ($current_page as $key => $term) { /** @var $term \Drupal\Core\Entity\EntityInterface */ $form['terms'][$key]['#term'] = $term; $indentation = array(); if (isset($term->depth) && $term->depth > 0) { $indentation = array('#theme' => 'indentation', '#size' => $term->depth); } $form['terms'][$key]['term'] = array('#prefix' => !empty($indentation) ? drupal_render($indentation) : '', '#type' => 'link', '#title' => $term->getName()) + $term->urlInfo()->toRenderArray(); if ($taxonomy_vocabulary->hierarchy != TAXONOMY_HIERARCHY_MULTIPLE && count($tree) > 1) { $parent_fields = TRUE; $form['terms'][$key]['term']['tid'] = array('#type' => 'hidden', '#value' => $term->id(), '#attributes' => array('class' => array('term-id'))); $form['terms'][$key]['term']['parent'] = array('#type' => 'hidden', '#default_value' => $term->parents[0], '#attributes' => array('class' => array('term-parent'))); $form['terms'][$key]['term']['depth'] = array('#type' => 'hidden', '#default_value' => $term->depth, '#attributes' => array('class' => array('term-depth'))); } $form['terms'][$key]['weight'] = array('#type' => 'weight', '#delta' => $delta, '#title' => $this->t('Weight for added term'), '#title_display' => 'invisible', '#default_value' => $term->getWeight(), '#attributes' => array('class' => array('term-weight'))); $operations = array('edit' => array('title' => $this->t('Edit'), 'query' => $destination) + $term->urlInfo('edit-form')->toArray(), 'delete' => array('title' => $this->t('Delete'), 'query' => $destination) + $term->urlInfo('delete-form')->toArray()); if ($this->moduleHandler->moduleExists('content_translation') && content_translation_translate_access($term)) { $operations['translate'] = array('title' => $this->t('Translate'), 'query' => $destination) + $term->urlInfo('drupal:content-translation-overview')->toArray(); } $form['terms'][$key]['operations'] = array('#type' => 'operations', '#links' => $operations); $form['terms'][$key]['#attributes']['class'] = array(); if ($parent_fields) { $form['terms'][$key]['#attributes']['class'][] = 'draggable'; } // Add classes that mark which terms belong to previous and next pages. if ($row_position < $back_step || $row_position >= $page_entries - $forward_step) { $form['terms'][$key]['#attributes']['class'][] = 'taxonomy-term-preview'; } if ($row_position !== 0 && $row_position !== count($tree) - 1) { if ($row_position == $back_step - 1 || $row_position == $page_entries - $forward_step - 1) { $form['terms'][$key]['#attributes']['class'][] = 'taxonomy-term-divider-top'; } elseif ($row_position == $back_step || $row_position == $page_entries - $forward_step) { $form['terms'][$key]['#attributes']['class'][] = 'taxonomy-term-divider-bottom'; } } // Add an error class if this row contains a form error. foreach ($errors as $error_key => $error) { if (strpos($error_key, $key) === 0) { $form['terms'][$key]['#attributes']['class'][] = 'error'; } } $row_position++; } if ($parent_fields) { $form['terms']['#tabledrag'][] = array('action' => 'match', 'relationship' => 'parent', 'group' => 'term-parent', 'subgroup' => 'term-parent', 'source' => 'term-id', 'hidden' => FALSE); $form['terms']['#tabledrag'][] = array('action' => 'depth', 'relationship' => 'group', 'group' => 'term-depth', 'hidden' => FALSE); $form['terms']['#attached']['library'][] = 'taxonomy/drupal.taxonomy'; $form['terms']['#attached']['js'][] = array('data' => array('taxonomy' => array('backStep' => $back_step, 'forwardStep' => $forward_step)), 'type' => 'setting'); } $form['terms']['#tabledrag'][] = array('action' => 'order', 'relationship' => 'sibling', 'group' => 'term-weight'); if ($taxonomy_vocabulary->hierarchy != TAXONOMY_HIERARCHY_MULTIPLE && count($tree) > 1) { $form['actions'] = array('#type' => 'actions', '#tree' => FALSE); $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Save'), '#button_type' => 'primary'); $form['actions']['reset_alphabetical'] = array('#type' => 'submit', '#submit' => array(array($this, 'submitReset')), '#value' => $this->t('Reset to alphabetical')); $form_state['redirect'] = array(current_path(), $page ? array('query' => array('page' => $page)) : array()); } return $form; }
/** * @see content_translation_translate_access() */ public function testContentTranslationOverviewAccess() { $access_control_handler = \Drupal::entityManager()->getAccessControlHandler('node'); $user = $this->createUser(['create content translations', 'access content']); $this->drupalLogin($user); $node = $this->drupalCreateNode(['status' => FALSE, 'type' => 'article']); $this->assertFalse(content_translation_translate_access($node)->isAllowed()); $access_control_handler->resetCache(); $node->setPublished(TRUE); $node->save(); $this->assertTrue(content_translation_translate_access($node)->isAllowed()); $access_control_handler->resetCache(); user_role_change_permissions(Role::AUTHENTICATED_ID, ['access content' => FALSE]); $user = $this->createUser(['create content translations']); $this->drupalLogin($user); $this->assertFalse(content_translation_translate_access($node)->isAllowed()); $access_control_handler->resetCache(); }
/** * Build the default links (reply, edit, delete …) for a comment. * * @param \Drupal\comment\CommentInterface $entity * The comment object. * @param \Drupal\Core\Entity\EntityInterface $commented_entity * The entity to which the comment is attached. * * @return array * An array that can be processed by drupal_pre_render_links(). */ protected static function buildLinks(CommentInterface $entity, EntityInterface $commented_entity) { $links = array(); $status = $commented_entity->get($entity->getFieldName())->status; $container = \Drupal::getContainer(); if ($status == CommentItemInterface::OPEN) { if ($entity->access('delete')) { $links['comment-delete'] = array('title' => t('Delete'), 'url' => $entity->urlInfo('delete-form'), 'html' => TRUE); } if ($entity->access('update')) { $links['comment-edit'] = array('title' => t('Edit'), 'url' => $entity->urlInfo('edit-form'), 'html' => TRUE); } if ($entity->access('create')) { $links['comment-reply'] = array('title' => t('Reply'), 'url' => Url::fromRoute('comment.reply', ['entity_type' => $entity->getCommentedEntityTypeId(), 'entity' => $entity->getCommentedEntityId(), 'field_name' => $entity->getFieldName(), 'pid' => $entity->id()]), 'html' => TRUE); } if (!$entity->isPublished() && $entity->access('approve')) { $links['comment-approve'] = array('title' => t('Approve'), 'url' => Url::fromRoute('comment.approve', ['comment' => $entity->id()]), 'html' => TRUE); } if (empty($links) && \Drupal::currentUser()->isAnonymous()) { $links['comment-forbidden']['title'] = \Drupal::service('comment.manager')->forbiddenMessage($commented_entity, $entity->getFieldName()); $links['comment-forbidden']['html'] = TRUE; } } // Add translations link for translation-enabled comment bundles. if (\Drupal::moduleHandler()->moduleExists('content_translation') && content_translation_translate_access($entity)->isAllowed()) { $links['comment-translations'] = array('title' => t('Translate'), 'url' => $entity->urlInfo('drupal:content-translation-overview'), 'html' => TRUE); } return array('#theme' => 'links__comment__comment', '#links' => $links, '#attributes' => array('class' => array('links', 'inline'))); }