filter_get_roles_by_format example

$this->assertTrue((bool) $format);
    $this->assertEquals('filter_test', $format->id());
    $this->assertEquals('Test format', $format->label());
    $this->assertEquals(2, $format->get('weight'));

    // Verify that format default property values have been added/injected.     $this->assertNotEmpty($format->uuid());

    // Verify that the loaded format does not contain any roles.     $this->assertNull($format->get('roles'));
    // Verify that the defined roles in the default config have been processed.     $this->assertEquals([RoleInterface::ANONYMOUS_ID, RoleInterface::AUTHENTICATED_ID]array_keys(filter_get_roles_by_format($format)));

    // Verify enabled filters.     $filters = $format->get('filters');
    $this->assertEquals(1, $filters['filter_html_escape']['status']);
    $this->assertEquals(-10, $filters['filter_html_escape']['weight']);
    $this->assertEquals('filter', $filters['filter_html_escape']['provider']);
    $this->assertEquals([]$filters['filter_html_escape']['settings']);
    $this->assertEquals(1, $filters['filter_autop']['status']);
    $this->assertEquals(0, $filters['filter_autop']['weight']);
    $this->assertEquals('filter', $filters['filter_autop']['provider']);
    $this->assertEquals([]$filters['filter_autop']['settings']);
    
'#type' => 'checkboxes',
      '#title' => $this->t('Roles'),
      '#options' => array_map('\Drupal\Component\Utility\Html::escape', 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));
    }

    // 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) {
        $this->messenger()->addWarning($this->t('The %filter filter is missing, and will be removed once this format is saved.', ['%filter' => $filter_id]));
        $filters->removeInstanceID($filter_id);
      }
/** * {@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;
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form$form_state);
    $this->messenger()->addStatus($this->t('The text format %format has been updated.', ['%format' => $this->entity->label()]));
    return $this->entity;
  }

}
$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);
  }

  /** * {@inheritdoc} */
  
/** * Tests if text format is available to a role. */
  public 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->assertContains($ridarray_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->assertNotContains($ridarray_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->assertContains($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->assertNotContains($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->assertEquals(filter_get_roles_by_format(FilterFormat::load(filter_fallback_format()))user_role_names(), 'All roles have access to the fallback format.');
    $this->assertContains(filter_fallback_format()array_keys(filter_get_formats_by_role($rid)), 'The fallback format appears in the list of allowed formats for any role.');
  }

  
Home | Imprint | This part of the site doesn't use cookies.