本文整理汇总了PHP中Drupal\filter\Entity\FilterFormat::load方法的典型用法代码示例。如果您正苦于以下问题:PHP FilterFormat::load方法的具体用法?PHP FilterFormat::load怎么用?PHP FilterFormat::load使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\filter\Entity\FilterFormat
的用法示例。
在下文中一共展示了FilterFormat::load方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testFilterFormat
/**
* Tests the Drupal 6 filter format to Drupal 8 migration.
*/
public function testFilterFormat()
{
$filter_format = FilterFormat::load('filtered_html');
// Check filter status.
$filters = $filter_format->get('filters');
$this->assertTrue($filters['filter_autop']['status']);
$this->assertTrue($filters['filter_url']['status']);
$this->assertTrue($filters['filter_htmlcorrector']['status']);
$this->assertTrue($filters['filter_html']['status']);
// These should be false by default.
$this->assertFalse(isset($filters['filter_html_escape']));
$this->assertFalse(isset($filters['filter_caption']));
$this->assertFalse(isset($filters['filter_html_image_secure']));
// Check variables migrated into filter.
$this->assertSame('<a href hreflang> <em> <strong> <cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd>', $filters['filter_html']['settings']['allowed_html']);
$this->assertSame(TRUE, $filters['filter_html']['settings']['filter_html_help']);
$this->assertSame(FALSE, $filters['filter_html']['settings']['filter_html_nofollow']);
$this->assertSame(72, $filters['filter_url']['settings']['filter_url_length']);
// Assert that the php_code format was migrated with filter_null in the
// php_code filter's place.
$filter_format = FilterFormat::load('php_code');
$this->assertInstanceOf(FilterFormatInterface::class, $filter_format);
$filters = $filter_format->get('filters');
$this->assertArrayHasKey('filter_null', $filters);
$this->assertArrayNotHasKey('php_code', $filters);
}
示例2: preRenderText
/**
* Pre-render callback: Renders a processed text element into #markup.
*
* Runs all the enabled filters on a piece of text.
*
* Note: Because filters can inject JavaScript or execute PHP code, security
* is vital here. When a user supplies a text format, you should validate it
* using $format->access() before accepting/using it. This is normally done in
* the validation stage of the Form API. You should for example never make a
* preview of content in a disallowed format.
*
* @param array $element
* A structured array with the following key-value pairs:
* - #text: containing the text to be filtered
* - #format: containing the machine name of the filter format to be used to
* filter the text. Defaults to the fallback format.
* - #langcode: the language code of the text to be filtered, e.g. 'en' for
* English. This allows filters to be language-aware so language-specific
* text replacement can be implemented. Defaults to an empty string.
* - #filter_types_to_skip: an array of filter types to skip, or an empty
* array (default) to skip no filter types. All of the format's filters
* will be applied, except for filters of the types that are marked to be
* skipped. FilterInterface::TYPE_HTML_RESTRICTOR is the only type that
* cannot be skipped.
*
* @return array
* The passed-in element with the filtered text in '#markup'.
*
* @ingroup sanitization
*/
public static function preRenderText($element)
{
$format_id = $element['#format'];
$filter_types_to_skip = $element['#filter_types_to_skip'];
$text = $element['#text'];
$langcode = $element['#langcode'];
if (!isset($format_id)) {
$format_id = static::configFactory()->get('filter.settings')->get('fallback_format');
}
/** @var \Drupal\filter\Entity\FilterFormat $format **/
$format = FilterFormat::load($format_id);
// If the requested text format doesn't exist or its disabled, the text
// cannot be filtered.
if (!$format || !$format->status()) {
$message = !$format ? 'Missing text format: %format.' : 'Disabled text format: %format.';
static::logger('filter')->alert($message, array('%format' => $format_id));
$element['#markup'] = '';
return $element;
}
$filter_must_be_applied = function (FilterInterface $filter) use($filter_types_to_skip) {
$enabled = $filter->status === TRUE;
$type = $filter->getType();
// Prevent FilterInterface::TYPE_HTML_RESTRICTOR from being skipped.
$filter_type_must_be_applied = $type == FilterInterface::TYPE_HTML_RESTRICTOR || !in_array($type, $filter_types_to_skip);
return $enabled && $filter_type_must_be_applied;
};
// Convert all Windows and Mac newlines to a single newline, so filters only
// need to deal with one possibility.
$text = str_replace(array("\r\n", "\r"), "\n", $text);
// Get a complete list of filters, ordered properly.
/** @var \Drupal\filter\Plugin\FilterInterface[] $filters **/
$filters = $format->filters();
// Give filters a chance to escape HTML-like data such as code or formulas.
foreach ($filters as $filter) {
if ($filter_must_be_applied($filter)) {
$text = $filter->prepare($text, $langcode);
}
}
// Perform filtering.
$metadata = BubbleableMetadata::createFromRenderArray($element);
foreach ($filters as $filter) {
if ($filter_must_be_applied($filter)) {
$result = $filter->process($text, $langcode);
$metadata = $metadata->merge($result);
$text = $result->getProcessedText();
}
}
// Filtering and sanitizing have been done in
// \Drupal\filter\Plugin\FilterInterface. $text is not guaranteed to be
// safe, but it has been passed through the filter system and checked with
// a text format, so it must be printed as is. (See the note about security
// in the method documentation above.)
$element['#markup'] = FilteredMarkup::create($text);
// Set the updated bubbleable rendering metadata and the text format's
// cache tag.
$metadata->applyTo($element);
$element['#cache']['tags'] = Cache::mergeTags($element['#cache']['tags'], $format->getCacheTags());
return $element;
}
示例3: preRenderTextFormat
/**
* Additional #pre_render callback for 'text_format' elements.
*/
function preRenderTextFormat(array $element)
{
// Allow modules to programmatically enforce no client-side editor by
// setting the #editor property to FALSE.
if (isset($element['#editor']) && !$element['#editor']) {
return $element;
}
// filter_process_format() copies properties to the expanded 'value' child
// element, including the #pre_render property. Skip this text format
// widget, if it contains no 'format'.
if (!isset($element['format'])) {
return $element;
}
$format_ids = array_keys($element['format']['format']['#options']);
// Early-return if no text editor is associated with any of the text formats.
$editors = Editor::loadMultiple($format_ids);
if (count($editors) === 0) {
return $element;
}
// Use a hidden element for a single text format.
$field_id = $element['value']['#id'];
if (!$element['format']['format']['#access']) {
// Use the first (and only) available text format.
$format_id = $format_ids[0];
$element['format']['editor'] = array('#type' => 'hidden', '#name' => $element['format']['format']['#name'], '#value' => $format_id, '#attributes' => array('class' => array('editor'), 'data-editor-for' => $field_id));
} else {
$element['format']['format']['#attributes']['class'][] = 'editor';
$element['format']['format']['#attributes']['data-editor-for'] = $field_id;
}
// Hide the text format's filters' guidelines of those text formats that have
// a text editor associated: they're rather useless when using a text editor.
foreach ($editors as $format_id => $editor) {
$element['format']['guidelines'][$format_id]['#access'] = FALSE;
}
// Attach Text Editor module's (this module) library.
$element['#attached']['library'][] = 'editor/drupal.editor';
// Attach attachments for all available editors.
$element['#attached'] = drupal_merge_attached($element['#attached'], $this->pluginManager->getAttachments($format_ids));
// Apply XSS filters when editing content if necessary. Some types of text
// editors cannot guarantee that the end user won't become a victim of XSS.
if (!empty($element['value']['#value'])) {
$original = $element['value']['#value'];
$format = FilterFormat::load($element['format']['format']['#value']);
// Ensure XSS-safety for the current text format/editor.
$filtered = editor_filter_xss($original, $format);
if ($filtered !== FALSE) {
$element['value']['#value'] = $filtered;
}
// Only when the user has access to multiple text formats, we must add data-
// attributes for the original value and change tracking, because they are
// only necessary when the end user can switch between text formats/editors.
if ($element['format']['format']['#access']) {
$element['value']['#attributes']['data-editor-value-is-changed'] = 'false';
$element['value']['#attributes']['data-editor-value-original'] = $original;
}
}
return $element;
}
示例4: setUp
/**
* {@inheritdoc}
*/
public function setUp()
{
parent::setUp();
$basic_html_format = FilterFormat::load('basic_html');
$restricted_html_format = FilterFormat::create(array('format' => 'restricted_html', 'name' => 'Restricted HTML'));
$restricted_html_format->save();
$full_html_format = FilterFormat::create(array('format' => 'full_html', 'name' => 'Full HTML'));
$full_html_format->save();
$this->loginAsAdmin(['access content overview', 'administer tmgmt', 'translate any entity', 'edit any translatable_node content', $basic_html_format->getPermissionName(), $restricted_html_format->getPermissionName(), $full_html_format->getPermissionName()]);
}
示例5: testDisableFallbackFormat
/**
* Tests disabling the fallback text format.
*/
public function testDisableFallbackFormat()
{
$this->installConfig(['filter']);
$message = '\\LogicException with message "The fallback text format \'plain_text\' cannot be disabled." was thrown.';
try {
FilterFormat::load('plain_text')->disable();
$this->fail($message);
} catch (\LogicException $e) {
$this->assertIdentical($e->getMessage(), "The fallback text format 'plain_text' cannot be disabled.", $message);
}
}
示例6: verifyTextFormat
/**
* Verifies that a text format is properly stored.
*/
function verifyTextFormat($format)
{
$t_args = array('%format' => $format->label());
$default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
// Verify the loaded filter has all properties.
$filter_format = FilterFormat::load($format->id());
$this->assertEqual($filter_format->id(), $format->id(), format_string('filter_format_load: Proper format id for text format %format.', $t_args));
$this->assertEqual($filter_format->label(), $format->label(), format_string('filter_format_load: Proper title for text format %format.', $t_args));
$this->assertEqual($filter_format->get('weight'), $format->get('weight'), format_string('filter_format_load: Proper weight for text format %format.', $t_args));
// Check that the filter was created in site default language.
$this->assertEqual($format->language()->getId(), $default_langcode, format_string('filter_format_load: Proper language code for text format %format.', $t_args));
}
示例7: testUpdateRoles
/**
* 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));
}
示例8: setUp
protected function setUp()
{
parent::setUp();
// Create Basic page node type.
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
/** @var \Drupal\filter\Entity\FilterFormat $filtered_html_format */
$filtered_html_format = FilterFormat::load('filtered_html');
$filtered_html_permission = $filtered_html_format->getPermissionName();
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, array($filtered_html_permission));
$this->adminUser = $this->drupalCreateUser(array('administer modules', 'administer filters', 'administer site configuration'));
$this->drupalLogin($this->adminUser);
}
示例9: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
/** @var \Drupal\filter\FilterFormatInterface $filter_test_format */
$filter_test_format = FilterFormat::load('filter_test');
/** @var \Drupal\filter\FilterFormatInterface $filtered_html_format */
$filtered_html_format = FilterFormat::load('filtered_html');
/** @var \Drupal\filter\FilterFormatInterface $full_html_format */
$full_html_format = FilterFormat::load('full_html');
// Create users.
$this->adminUser = $this->drupalCreateUser(array('administer filters', $filtered_html_format->getPermissionName(), $full_html_format->getPermissionName(), $filter_test_format->getPermissionName()));
$this->webUser = $this->drupalCreateUser(array($filtered_html_format->getPermissionName(), $filter_test_format->getPermissionName()));
}
示例10: preRenderText
/**
* Pre-render callback: Renders a processed text element into #markup.
*
* Runs all the enabled filters on a piece of text.
*
* Note: Because filters can inject JavaScript or execute PHP code, security
* is vital here. When a user supplies a text format, you should validate it
* using $format->access() before accepting/using it. This is normally done in
* the validation stage of the Form API. You should for example never make a
* preview of content in a disallowed format.
*
* @param array $element
* A structured array with the following key-value pairs:
* - #text: containing the text to be filtered
* - #format: containing the machine name of the filter format to be used to
* filter the text. Defaults to the fallback format.
* - #langcode: the language code of the text to be filtered, e.g. 'en' for
* English. This allows filters to be language-aware so language-specific
* text replacement can be implemented. Defaults to an empty string.
* - #filter_types_to_skip: an array of filter types to skip, or an empty
* array (default) to skip no filter types. All of the format's filters
* will be applied, except for filters of the types that are marked to be
* skipped. FilterInterface::TYPE_HTML_RESTRICTOR is the only type that
* cannot be skipped.
*
* @return array
* The passed-in element with the filtered text in '#markup'.
*
* @ingroup sanitization
*/
public static function preRenderText($element)
{
$format_id = $element['#format'];
$filter_types_to_skip = $element['#filter_types_to_skip'];
$text = $element['#text'];
$langcode = $element['#langcode'];
if (!isset($format_id)) {
$format_id = static::configFactory()->get('filter.settings')->get('fallback_format');
}
// If the requested text format does not exist, the text cannot be filtered.
/** @var \Drupal\filter\Entity\FilterFormat $format **/
if (!($format = FilterFormat::load($format_id))) {
static::logger('filter')->alert('Missing text format: %format.', array('%format' => $format_id));
$element['#markup'] = '';
return $element;
}
$filter_must_be_applied = function (FilterInterface $filter) use($filter_types_to_skip) {
$enabled = $filter->status === TRUE;
$type = $filter->getType();
// Prevent FilterInterface::TYPE_HTML_RESTRICTOR from being skipped.
$filter_type_must_be_applied = $type == FilterInterface::TYPE_HTML_RESTRICTOR || !in_array($type, $filter_types_to_skip);
return $enabled && $filter_type_must_be_applied;
};
// Convert all Windows and Mac newlines to a single newline, so filters only
// need to deal with one possibility.
$text = str_replace(array("\r\n", "\r"), "\n", $text);
// Get a complete list of filters, ordered properly.
/** @var \Drupal\filter\Plugin\FilterInterface[] $filters **/
$filters = $format->filters();
// Give filters a chance to escape HTML-like data such as code or formulas.
foreach ($filters as $filter) {
if ($filter_must_be_applied($filter)) {
$text = $filter->prepare($text, $langcode);
}
}
// Perform filtering.
$metadata = BubbleableMetadata::createFromRenderArray($element);
foreach ($filters as $filter) {
if ($filter_must_be_applied($filter)) {
$result = $filter->process($text, $langcode);
$metadata = $metadata->merge($result);
$text = $result->getProcessedText();
}
}
// Filtering done, store in #markup, set the updated bubbleable rendering
// metadata, and set the text format's cache tag.
$element['#markup'] = $text;
$metadata->applyTo($element);
$element['#cache']['tags'] = Cache::mergeTags($element['#cache']['tags'], $format->getCacheTags());
return $element;
}
示例11: testFormatters
/**
* Tests all text field formatters.
*/
public function testFormatters()
{
$formatters = array('text_default', 'text_trimmed', 'text_summary_or_trimmed');
// Create the entity to be referenced.
$entity = entity_create($this->entityType, array('name' => $this->randomMachineName()));
$entity->formatted_text = array('value' => 'Hello, world!', 'format' => 'my_text_format');
$entity->save();
foreach ($formatters as $formatter) {
// Verify the text field formatter's render array.
$build = $entity->get('formatted_text')->view(array('type' => $formatter));
drupal_render($build[0]);
$this->assertEqual($build[0]['#markup'], "<p>Hello, world!</p>\n");
$this->assertEqual($build[0]['#cache']['tags'], FilterFormat::load('my_text_format')->getCacheTag(), format_string('The @formatter formatter has the expected cache tags when formatting a formatted text field.', array('@formatter' => $formatter)));
}
}
示例12: testAllowedHtmlUpdate
/**
* Tests system_update_8009().
*/
public function testAllowedHtmlUpdate()
{
// Make sure we have the expected values before the update.
$filters_before = ['basic_html' => '<a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <h4> <h5> <h6> <p> <br> <span> <img>', 'restricted_html' => '<a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <h4> <h5> <h6>'];
foreach ($filters_before as $name => $before) {
$config = FilterFormat::load($name)->toArray();
$this->assertIdentical($before, $config['filters']['filter_html']['settings']['allowed_html']);
}
$this->runUpdates();
// Make sure we have the expected values after the update.
$filters_after = ['basic_html' => '<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h4 id> <h5 id> <h6 id> <p> <br> <span> <img src alt height width data-align data-caption>', 'restricted_html' => '<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h4 id> <h5 id> <h6 id>'];
foreach ($filters_after as $name => $after) {
$config = FilterFormat::load($name)->toArray();
$this->assertIdentical($after, $config['filters']['filter_html']['settings']['allowed_html']);
}
}
示例13: testFilterFormat
/**
* Tests the Drupal 7 filter format to Drupal 8 migration.
*/
public function testFilterFormat()
{
$this->assertEntity('custom_text_format', 'Custom Text format', ['filter_autop', 'filter_html']);
$this->assertEntity('filtered_html', 'Filtered HTML', ['filter_autop', 'filter_html', 'filter_htmlcorrector', 'filter_url']);
$this->assertEntity('full_html', 'Full HTML', ['filter_autop', 'filter_htmlcorrector', 'filter_url']);
$this->assertEntity('plain_text', 'Plain text', ['filter_html_escape', 'filter_url', 'filter_autop']);
// This assertion covers issue #2555089. Drupal 7 formats are identified
// by machine names, so migrated formats should be merged into existing
// ones.
$this->assertNull(FilterFormat::load('plain_text1'));
// Ensure that filter-specific settings were migrated.
/** @var \Drupal\filter\FilterFormatInterface $format */
$format = FilterFormat::load('filtered_html');
$config = $format->filters('filter_html')->getConfiguration();
$this->assertIdentical('<div> <span> <ul> <li>', $config['settings']['allowed_html']);
$config = $format->filters('filter_url')->getConfiguration();
$this->assertIdentical(128, $config['settings']['filter_url_length']);
}
示例14: testDefaultTextFormats
/**
* Tests if the default text format is accessible to users.
*/
function testDefaultTextFormats()
{
// Create two text formats, and two users. The first user has access to
// both formats, but the second user only has access to the second one.
$admin_user = $this->drupalCreateUser(array('administer filters'));
$this->drupalLogin($admin_user);
$formats = array();
for ($i = 0; $i < 2; $i++) {
$edit = array('format' => Unicode::strtolower($this->randomMachineName()), 'name' => $this->randomMachineName());
$this->drupalPostForm('admin/config/content/formats/add', $edit, t('Save configuration'));
$this->resetFilterCaches();
$formats[] = FilterFormat::load($edit['format']);
}
list($first_format, $second_format) = $formats;
$second_format_permission = $second_format->getPermissionName();
$first_user = $this->drupalCreateUser(array($first_format->getPermissionName(), $second_format_permission));
$second_user = $this->drupalCreateUser(array($second_format_permission));
// Adjust the weights so that the first and second formats (in that order)
// are the two lowest weighted formats available to any user.
$edit = array();
$edit['formats[' . $first_format->id() . '][weight]'] = -2;
$edit['formats[' . $second_format->id() . '][weight]'] = -1;
$this->drupalPostForm('admin/config/content/formats', $edit, t('Save'));
$this->resetFilterCaches();
// Check that each user's default format is the lowest weighted format that
// the user has access to.
$actual = filter_default_format($first_user);
$expected = $first_format->id();
$this->assertEqual($actual, $expected, "First user's default format {$actual} is the expected lowest weighted format {$expected} that the user has access to.");
$actual = filter_default_format($second_user);
$expected = $second_format->id();
$this->assertEqual($actual, $expected, "Second user's default format {$actual} is the expected lowest weighted format {$expected} that the user has access to, and different to the first user's.");
// Reorder the two formats, and check that both users now have the same
// default.
$edit = array();
$edit['formats[' . $second_format->id() . '][weight]'] = -3;
$this->drupalPostForm('admin/config/content/formats', $edit, t('Save'));
$this->resetFilterCaches();
$this->assertEqual(filter_default_format($first_user), filter_default_format($second_user), 'After the formats are reordered, both users have the same default format.');
}
示例15: testFilterFormat
/**
* Tests the Drupal 7 filter format to Drupal 8 migration.
*/
public function testFilterFormat()
{
$this->assertEntity('custom_text_format', 'Custom Text format', ['filter_autop', 'filter_html']);
$this->assertEntity('filtered_html', 'Filtered HTML', ['filter_autop', 'filter_html', 'filter_htmlcorrector', 'filter_url']);
$this->assertEntity('full_html', 'Full HTML', ['filter_autop', 'filter_htmlcorrector', 'filter_url']);
$this->assertEntity('plain_text', 'Plain text', ['filter_html_escape', 'filter_url', 'filter_autop']);
// This assertion covers issue #2555089. Drupal 7 formats are identified
// by machine names, so migrated formats should be merged into existing
// ones.
$this->assertNull(FilterFormat::load('plain_text1'));
// Ensure that filter-specific settings were migrated.
/** @var \Drupal\filter\FilterFormatInterface $format */
$format = FilterFormat::load('filtered_html');
$config = $format->filters('filter_html')->getConfiguration();
$this->assertIdentical('<div> <span> <ul type> <li> <ol start type> <a href hreflang> <img src alt height width>', $config['settings']['allowed_html']);
$config = $format->filters('filter_url')->getConfiguration();
$this->assertIdentical(128, $config['settings']['filter_url_length']);
// The php_code format gets migrated, but the php_code filter is changed to
// filter_null.
$filters = FilterFormat::load('php_code')->get('filters');
$this->assertTrue(isset($filters['filter_null']));
}