/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $format = $this->entity; $is_fallback = $format->id() == $this->config('filter.settings')->get('fallback_format'); $form['#tree'] = TRUE; $form['#attached']['library'][] = 'filter/drupal.filter.admin'; $form['name'] = array('#type' => 'textfield', '#title' => $this->t('Name'), '#default_value' => $format->label(), '#required' => TRUE, '#weight' => -30); $form['format'] = array('#type' => 'machine_name', '#required' => TRUE, '#default_value' => $format->id(), '#maxlength' => 255, '#machine_name' => array('exists' => array($this, 'exists'), 'source' => array('name')), '#disabled' => !$format->isNew(), '#weight' => -20); // Add user role access selection. $form['roles'] = array('#type' => 'checkboxes', '#title' => $this->t('Roles'), '#options' => array_map('\\Drupal\\Component\\Utility\\String::checkPlain', user_role_names()), '#disabled' => $is_fallback, '#weight' => -10); if ($is_fallback) { $form['roles']['#description'] = $this->t('All roles for this text format must be enabled and cannot be changed.'); } if (!$format->isNew()) { // If editing an existing text format, pre-select its current permissions. $form['roles']['#default_value'] = array_keys(filter_get_roles_by_format($format)); } elseif ($admin_role = $this->config('user.settings')->get('admin_role')) { // If adding a new text format and the site has an administrative role, // pre-select that role so as to grant administrators access to the new // text format permission by default. $form['roles']['#default_value'] = array($admin_role); } // Create filter plugin instances for all available filters, including both // enabled/configured ones as well as new and not yet unconfigured ones. $filters = $format->filters(); foreach ($filters as $filter_id => $filter) { // When a filter is missing, it is replaced by the null filter. Remove it // here, so that saving the form will remove the missing filter. if ($filter instanceof FilterNull) { drupal_set_message($this->t('The %filter filter is missing, and will be removed once this format is saved.', array('%filter' => $filter_id)), 'warning'); $filters->removeInstanceID($filter_id); } } // Filter status. $form['filters']['status'] = array('#type' => 'item', '#title' => $this->t('Enabled filters'), '#prefix' => '<div id="filters-status-wrapper">', '#suffix' => '</div>', '#input' => FALSE); // Filter order (tabledrag). $form['filters']['order'] = array('#type' => 'table', '#attributes' => array('id' => 'filter-order'), '#title' => $this->t('Filter processing order'), '#tabledrag' => array(array('action' => 'order', 'relationship' => 'sibling', 'group' => 'filter-order-weight')), '#tree' => FALSE, '#input' => FALSE, '#theme_wrappers' => array('form_element')); // Filter settings. $form['filter_settings'] = array('#type' => 'vertical_tabs', '#title' => $this->t('Filter settings')); foreach ($filters as $name => $filter) { $form['filters']['status'][$name] = array('#type' => 'checkbox', '#title' => $filter->getLabel(), '#default_value' => $filter->status, '#parents' => array('filters', $name, 'status'), '#description' => $filter->getDescription(), '#weight' => $filter->weight); $form['filters']['order'][$name]['#attributes']['class'][] = 'draggable'; $form['filters']['order'][$name]['#weight'] = $filter->weight; $form['filters']['order'][$name]['filter'] = array('#markup' => $filter->getLabel()); $form['filters']['order'][$name]['weight'] = array('#type' => 'weight', '#title' => $this->t('Weight for @title', array('@title' => $filter->getLabel())), '#title_display' => 'invisible', '#delta' => 50, '#default_value' => $filter->weight, '#parents' => array('filters', $name, 'weight'), '#attributes' => array('class' => array('filter-order-weight'))); // Retrieve the settings form of the filter plugin. The plugin should not be // aware of the text format. Therefore, it only receives a set of minimal // base properties to allow advanced implementations to work. $settings_form = array('#parents' => array('filters', $name, 'settings'), '#tree' => TRUE); $settings_form = $filter->settingsForm($settings_form, $form_state); if (!empty($settings_form)) { $form['filters']['settings'][$name] = array('#type' => 'details', '#title' => $filter->getLabel(), '#open' => TRUE, '#weight' => $filter->weight, '#parents' => array('filters', $name, 'settings'), '#group' => 'filter_settings'); $form['filters']['settings'][$name] += $settings_form; } } return parent::form($form, $form_state); }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { if (!$this->entity->status()) { throw new NotFoundHttpException(); } $form['#title'] = $this->entity->label(); $form = parent::form($form, $form_state); $form['roles']['#default_value'] = array_keys(filter_get_roles_by_format($this->entity)); return $form; }
/** * Tests that changes to FilterFormat::$roles do not have an effect. */ function testUpdateRoles() { // Verify role permissions declared in default config. $format = entity_load('filter_format', 'filter_test'); $this->assertEqual(array_keys(filter_get_roles_by_format($format)), array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID)); // Attempt to change roles. $format->set('roles', array(DRUPAL_AUTHENTICATED_RID)); $format->save(); // Verify that roles have not been updated. $format = entity_load('filter_format', 'filter_test'); $this->assertEqual(array_keys(filter_get_roles_by_format($format)), array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID)); }
/** * Tests that changes to FilterFormat::$roles do not have an effect. */ function testUpdateRoles() { // Verify role permissions declared in default config. $format = FilterFormat::load('filter_test'); $this->assertEqual(array_keys(filter_get_roles_by_format($format)), array(RoleInterface::ANONYMOUS_ID, RoleInterface::AUTHENTICATED_ID)); // Attempt to change roles. $format->set('roles', array(RoleInterface::AUTHENTICATED_ID)); $format->save(); // Verify that roles have not been updated. $format = FilterFormat::load('filter_test'); $this->assertEqual(array_keys(filter_get_roles_by_format($format)), array(RoleInterface::ANONYMOUS_ID, RoleInterface::AUTHENTICATED_ID)); }
/** * Tests if text format is available to a role. */ function testFormatRoles() { // Get the role ID assigned to the regular user. $roles = $this->webUser->getRoles(TRUE); $rid = $roles[0]; // Check that this role appears in the list of roles that have access to an // allowed text format, but does not appear in the list of roles that have // access to a disallowed text format. $this->assertTrue(in_array($rid, array_keys(filter_get_roles_by_format($this->allowedFormat))), 'A role which has access to a text format appears in the list of roles that have access to that format.'); $this->assertFalse(in_array($rid, array_keys(filter_get_roles_by_format($this->disallowedFormat))), 'A role which does not have access to a text format does not appear in the list of roles that have access to that format.'); // Check that the correct text format appears in the list of formats // available to that role. $this->assertTrue(in_array($this->allowedFormat->id(), array_keys(filter_get_formats_by_role($rid))), 'A text format which a role has access to appears in the list of formats available to that role.'); $this->assertFalse(in_array($this->disallowedFormat->id(), array_keys(filter_get_formats_by_role($rid))), 'A text format which a role does not have access to does not appear in the list of formats available to that role.'); // Check that the fallback format is always allowed. $this->assertEqual(filter_get_roles_by_format(FilterFormat::load(filter_fallback_format())), user_role_names(), 'All roles have access to the fallback format.'); $this->assertTrue(in_array(filter_fallback_format(), array_keys(filter_get_formats_by_role($rid))), 'The fallback format appears in the list of allowed formats for any role.'); }
/** * {@inheritdoc} */ public function buildRow(EntityInterface $entity) { // Check whether this is the fallback text format. This format is available // to all roles and cannot be disabled via the admin interface. if ($entity->isFallbackFormat()) { $row['label'] = SafeMarkup::placeholder($entity->label()); $fallback_choice = $this->configFactory->get('filter.settings')->get('always_show_fallback_choice'); if ($fallback_choice) { $roles_markup = SafeMarkup::placeholder($this->t('All roles may use this format')); } else { $roles_markup = SafeMarkup::placeholder($this->t('This format is shown when no other formats are available')); } } else { $row['label'] = $this->getLabel($entity); $roles = array_map('\\Drupal\\Component\\Utility\\SafeMarkup::checkPlain', filter_get_roles_by_format($entity)); $roles_markup = $roles ? implode(', ', $roles) : $this->t('No roles may use this format'); } $row['roles'] = !empty($this->weightKey) ? array('#markup' => $roles_markup) : $roles_markup; return $row + parent::buildRow($entity); }
/** * {@inheritdoc} */ public function buildRow(EntityInterface $entity) { // Check whether this is the fallback text format. This format is available // to all roles and cannot be disabled via the admin interface. $row['label'] = $entity->label(); $row['roles'] = []; if ($entity->isFallbackFormat()) { $fallback_choice = $this->configFactory->get('filter.settings')->get('always_show_fallback_choice'); if ($fallback_choice) { $row['roles']['#markup'] = $this->t('All roles may use this format'); } else { $row['roles']['#markup'] = $this->t('This format is shown when no other formats are available'); } // Emphasize the fallback role text since it is important to understand // how it works which configuring filter formats. Additionally, it is not // a list of roles unlike the other values in this column. $row['roles']['#prefix'] = '<em>'; $row['roles']['#suffix'] = '</em>'; } else { $row['roles'] = ['#theme' => 'item_list', '#items' => filter_get_roles_by_format($entity), '#empty' => $this->t('No roles may use this format'), '#context' => ['list_style' => 'comma-list']]; } return $row + parent::buildRow($entity); }
/** * Retrieves a list of roles for a given text format. * * @param string $format_name * Text format machine name. * * @return array * An array of role names, keyed by role ID. */ public function getFormatRoles($format_name) { $format = $this->getFormat($format_name); return filter_get_roles_by_format($format); }
/** * @return array */ protected function checkPhpFilter() { $result = TRUE; $check_result_value = array(); $formats = \Drupal::entityManager()->getStorage('filter_format')->loadByProperties(array('status' => TRUE)); // Check formats that are accessible by untrusted users. $untrusted_roles = $this->untrustedRoles(); $untrusted_roles = array_keys($untrusted_roles); foreach ($formats as $id => $format) { $format_roles = filter_get_roles_by_format($format); $intersect = array_intersect(array_keys($format_roles), $untrusted_roles); if (!empty($intersect)) { // Untrusted users can use this format. $filters = $formats[$id]->get('filters'); // Check format for enabled PHP filter. if (in_array('php_code', array_keys($filters)) && $filters['php_code']['status'] == 1) { $result = FALSE; $check_result_value['formats'][$id] = $format; } } } return array('result' => $result, 'value' => $check_result_value); }
/** * Returns the selected Medium Editor id for an account from editor settings. */ public static function getMediumId(Editor $editor, AccountInterface $account) { $settings = $editor->getSettings(); if (!empty($settings['roles_editors'])) { // Filter roles in two steps. May avoid a db hit by filter_get_roles_by_format(). if ($roles_editors = array_intersect_key($settings['roles_editors'], array_flip($account->getRoles()))) { if ($roles_editors = array_intersect_key($roles_editors, filter_get_roles_by_format($editor->getFilterFormat()))) { return reset($roles_editors); } } } return $settings['default_editor']; }
/** * {@inheritdoc} */ public function buildRow(EntityInterface $entity) { // Check whether this is the fallback text format. This format is available // to all roles and cannot be disabled via the admin interface. $row['label'] = $this->getLabel($entity); $row['roles'] = []; if ($entity->isFallbackFormat()) { $fallback_choice = $this->configFactory->get('filter.settings')->get('always_show_fallback_choice'); if ($fallback_choice) { $roles_markup = $this->t('All roles may use this format'); } else { $roles_markup = $this->t('This format is shown when no other formats are available'); } // Emphasize the fallback role text since it is important to understand // how it works which configuring filter formats. Additionally, it is not // a list of roles unlike the other values in this column. $row['roles']['#prefix'] = '<em>'; $row['roles']['#suffix'] = '</em>'; } else { $roles = array_map('\\Drupal\\Component\\Utility\\SafeMarkup::checkPlain', filter_get_roles_by_format($entity)); $roles_markup = $roles ? implode(', ', $roles) : $this->t('No roles may use this format'); } $row['roles']['#markup'] = $roles_markup; return $row + parent::buildRow($entity); }