本文整理汇总了PHP中Drupal\Component\Serialization\Json::encode方法的典型用法代码示例。如果您正苦于以下问题:PHP Json::encode方法的具体用法?PHP Json::encode怎么用?PHP Json::encode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Component\Serialization\Json
的用法示例。
在下文中一共展示了Json::encode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: selectCondition
/**
* Presents a list of conditions to add to the block_visibility_group entity.
*
* @param \Drupal\block_visibility_groups\Entity\BlockVisibilityGroup $block_visibility_group
* The block_visibility_group entity.
*
* @return array
* The condition selection page.
*/
public function selectCondition(BlockVisibilityGroup $block_visibility_group, $redirect) {
$build = [
'#theme' => 'links',
'#links' => [],
];
$available_plugins = $this->conditionManager->getDefinitions();
// @todo Should nesting Conditions be allowed
unset($available_plugins['condition_group']);
foreach ($available_plugins as $condition_id => $condition) {
$build['#links'][$condition_id] = [
'title' => $condition['label'],
'url' => Url::fromRoute('block_visibility_groups.condition_add', [
'block_visibility_group' => $block_visibility_group->id(),
'condition_id' => $condition_id,
'redirect' => $redirect,
]),
'attributes' => [
'class' => ['use-ajax'],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 'auto',
]),
],
];
}
return $build;
}
示例2: testFileFieldFormatter
/**
* Tests file field formatter Entity Embed Display plugins.
*/
public function testFileFieldFormatter()
{
// Ensure that file field formatters are available as plugins.
$this->assertAvailableDisplayPlugins($this->file, ['entity_reference:entity_reference_label', 'entity_reference:entity_reference_entity_id', 'file:file_default', 'file:file_table', 'file:file_url_plain']);
// Ensure that correct form attributes are returned for the file field
// formatter plugins.
$form = array();
$form_state = new FormState();
$plugins = array('file:file_table', 'file:file_default', 'file:file_url_plain');
// Ensure that description field is available for all the 'file' plugins.
foreach ($plugins as $plugin) {
$display = $this->displayPluginManager()->createInstance($plugin, array());
$display->setContextValue('entity', $this->file);
$conf_form = $display->buildConfigurationForm($form, $form_state);
$this->assertIdentical(array_keys($conf_form), array('description'));
$this->assertIdentical($conf_form['description']['#type'], 'textfield');
$this->assertIdentical((string) $conf_form['description']['#title'], 'Description');
}
// Test entity embed using 'Generic file' Entity Embed Display plugin.
$embed_settings = array('description' => "This is sample description");
$content = '<drupal-entity data-entity-type="file" data-entity-uuid="' . $this->file->uuid() . '" data-entity-embed-display="file:file_default" data-entity-embed-settings=\'' . Json::encode($embed_settings) . '\'>This placeholder should not be rendered.</drupal-entity>';
$settings = array();
$settings['type'] = 'page';
$settings['title'] = 'Test entity embed with file:file_default';
$settings['body'] = array(array('value' => $content, 'format' => 'custom_format'));
$node = $this->drupalCreateNode($settings);
$this->drupalGet('node/' . $node->id());
$this->assertText($embed_settings['description'], 'Description of the embedded file exists in page.');
$this->assertNoText(strip_tags($content), 'Placeholder does not appears in the output when embed is successful.');
$this->assertLinkByHref(file_create_url($this->file->getFileUri()), 0, 'Link to the embedded file exists.');
}
示例3: testRead
/**
* Tests several valid and invalid read requests on all entity types.
*/
public function testRead()
{
// @todo Expand this at least to users.
// Define the entity types we want to test.
$entity_types = array('entity_test', 'node');
foreach ($entity_types as $entity_type) {
$this->enableService('entity:' . $entity_type, 'GET');
// Create a user account that has the required permissions to read
// resources via the REST API.
$permissions = $this->entityPermissions($entity_type, 'view');
$permissions[] = 'restful get entity:' . $entity_type;
$account = $this->drupalCreateUser($permissions);
$this->drupalLogin($account);
// Create an entity programmatically.
$entity = $this->entityCreate($entity_type);
$entity->save();
// Read it over the REST API.
$response = $this->httpRequest($entity->getSystemPath(), 'GET', NULL, $this->defaultMimeType);
$this->assertResponse('200', 'HTTP response code is correct.');
$this->assertHeader('content-type', $this->defaultMimeType);
$data = Json::decode($response);
// Only assert one example property here, other properties should be
// checked in serialization tests.
$this->assertEqual($data['uuid'][0]['value'], $entity->uuid(), 'Entity UUID is correct');
// Try to read the entity with an unsupported mime format.
$response = $this->httpRequest($entity->getSystemPath(), 'GET', NULL, 'application/wrongformat');
$this->assertResponse(200);
$this->assertHeader('Content-type', 'text/html; charset=UTF-8');
// Try to read an entity that does not exist.
$response = $this->httpRequest($entity_type . '/9999', 'GET', NULL, $this->defaultMimeType);
$this->assertResponse(404);
$path = $entity_type == 'node' ? '/node/{node}' : '/entity_test/{entity_test}';
$expected_message = Json::encode(['error' => 'A fatal error occurred: The "' . $entity_type . '" parameter was not converted for the path "' . $path . '" (route name: "rest.entity.' . $entity_type . '.GET.hal_json")']);
$this->assertIdentical($expected_message, $response, 'Response message is correct.');
// Make sure that field level access works and that the according field is
// not available in the response. Only applies to entity_test.
// @see entity_test_entity_field_access()
if ($entity_type == 'entity_test') {
$entity->field_test_text->value = 'no access value';
$entity->save();
$response = $this->httpRequest($entity->getSystemPath(), 'GET', NULL, $this->defaultMimeType);
$this->assertResponse(200);
$this->assertHeader('content-type', $this->defaultMimeType);
$data = Json::decode($response);
$this->assertFalse(isset($data['field_test_text']), 'Field access protected field is not visible in the response.');
}
// Try to read an entity without proper permissions.
$this->drupalLogout();
$response = $this->httpRequest($entity->getSystemPath(), 'GET', NULL, $this->defaultMimeType);
$this->assertResponse(403);
$this->assertIdentical('{}', $response);
}
// Try to read a resource which is not REST API enabled.
$account = $this->drupalCreateUser();
$this->drupalLogin($account);
$response = $this->httpRequest($account->getSystemPath(), 'GET', NULL, $this->defaultMimeType);
$this->assertResponse(404);
$expected_message = Json::encode(['error' => 'A fatal error occurred: Unable to find the controller for path "/user/4". Maybe you forgot to add the matching route in your routing configuration?']);
$this->assertIdentical($expected_message, $response);
}
示例4: testPiwikCustomVariables
/**
* Tests if custom variables are properly added to the page.
*/
public function testPiwikCustomVariables()
{
$site_id = '3';
$this->config('piwik.settings')->set('site_id', $site_id)->save();
$this->config('piwik.settings')->set('url_http', 'http://www.example.com/piwik/')->save();
$this->config('piwik.settings')->set('url_https', 'https://www.example.com/piwik/')->save();
// Basic test if the feature works.
$custom_vars = [1 => ['slot' => 1, 'name' => 'Foo 1', 'value' => 'Bar 1', 'scope' => 'visit'], 2 => ['slot' => 2, 'name' => 'Foo 2', 'value' => 'Bar 2', 'scope' => 'page'], 3 => ['slot' => 3, 'name' => 'Foo 3', 'value' => 'Bar 3', 'scope' => 'page'], 4 => ['slot' => 4, 'name' => 'Foo 4', 'value' => 'Bar 4', 'scope' => 'visit'], 5 => ['slot' => 5, 'name' => 'Foo 5', 'value' => 'Bar 5', 'scope' => 'visit']];
$this->config('piwik.settings')->set('custom.variable', $custom_vars)->save();
$this->drupalGet('');
foreach ($custom_vars as $slot) {
$this->assertRaw('_paq.push(["setCustomVariable", ' . Json::encode($slot['slot']) . ', ' . Json::encode($slot['name']) . ', ' . Json::encode($slot['value']) . ', ' . Json::encode($slot['scope']) . ']);', '[testPiwikCustomVariables]: setCustomVariable ' . $slot['slot'] . ' is shown.');
}
// Test whether tokens are replaced in custom variable names.
$site_slogan = $this->randomMachineName(16);
$this->config('system.site')->set('slogan', $site_slogan)->save();
$custom_vars = [1 => ['slot' => 1, 'name' => 'Name: [site:slogan]', 'value' => 'Value: [site:slogan]', 'scope' => 'visit'], 2 => ['slot' => 2, 'name' => '', 'value' => $this->randomMachineName(16), 'scope' => 'page'], 3 => ['slot' => 3, 'name' => $this->randomMachineName(16), 'value' => '', 'scope' => 'visit'], 4 => ['slot' => 4, 'name' => '', 'value' => '', 'scope' => 'page'], 5 => ['slot' => 5, 'name' => '', 'value' => '', 'scope' => 'visit']];
$this->config('piwik.settings')->set('custom.variable', $custom_vars)->save();
$this->verbose('<pre>' . print_r($custom_vars, TRUE) . '</pre>');
$this->drupalGet('');
$this->assertRaw('_paq.push(["setCustomVariable", 1, ' . Json::encode("Name: {$site_slogan}") . ', ' . Json::encode("Value: {$site_slogan}") . ', "visit"]', '[testPiwikCustomVariables]: Tokens have been replaced in custom variable.');
$this->assertNoRaw('_paq.push(["setCustomVariable", 2,', '[testPiwikCustomVariables]: Value with empty name is not shown.');
$this->assertNoRaw('_paq.push(["setCustomVariable", 3,', '[testPiwikCustomVariables]: Name with empty value is not shown.');
$this->assertNoRaw('_paq.push(["setCustomVariable", 4,', '[testPiwikCustomVariables]: Empty name and value is not shown.');
$this->assertNoRaw('_paq.push(["setCustomVariable", 5,', '[testPiwikCustomVariables]: Empty name and value is not shown.');
}
示例5: buildForm
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, PageInterface $page = NULL, $display_variant_id = NULL)
{
$form = parent::buildForm($form, $form_state, $page, $display_variant_id);
// Set up the attributes used by a modal to prevent duplication later.
$attributes = ['class' => ['use-ajax'], 'data-dialog-type' => 'modal', 'data-dialog-options' => Json::encode(['width' => 'auto'])];
$add_button_attributes = NestedArray::mergeDeep($attributes, ['class' => ['button', 'button--small', 'button-action']]);
if ($this->displayVariant instanceof ConditionVariantInterface) {
if ($selection_conditions = $this->displayVariant->getSelectionConditions()) {
// Selection conditions.
$form['selection_section'] = ['#type' => 'details', '#title' => $this->t('Selection Conditions'), '#open' => TRUE];
$form['selection_section']['add'] = ['#type' => 'link', '#title' => $this->t('Add new selection condition'), '#url' => Url::fromRoute('page_manager.selection_condition_select', ['page' => $this->page->id(), 'display_variant_id' => $this->displayVariant->id()]), '#attributes' => $add_button_attributes, '#attached' => ['library' => ['core/drupal.ajax']]];
$form['selection_section']['table'] = ['#type' => 'table', '#header' => [$this->t('Label'), $this->t('Description'), $this->t('Operations')], '#empty' => $this->t('There are no selection conditions.')];
$form['selection_section']['selection_logic'] = ['#type' => 'radios', '#options' => ['and' => $this->t('All conditions must pass'), 'or' => $this->t('Only one condition must pass')], '#default_value' => $this->displayVariant->getSelectionLogic()];
$form['selection_section']['selection'] = ['#tree' => TRUE];
foreach ($selection_conditions as $selection_id => $selection_condition) {
$row = [];
$row['label']['#markup'] = $selection_condition->getPluginDefinition()['label'];
$row['description']['#markup'] = $selection_condition->summary();
$operations = [];
$operations['edit'] = ['title' => $this->t('Edit'), 'url' => Url::fromRoute('page_manager.selection_condition_edit', ['page' => $this->page->id(), 'display_variant_id' => $this->displayVariant->id(), 'condition_id' => $selection_id]), 'attributes' => $attributes];
$operations['delete'] = ['title' => $this->t('Delete'), 'url' => Url::fromRoute('page_manager.selection_condition_delete', ['page' => $this->page->id(), 'display_variant_id' => $this->displayVariant->id(), 'condition_id' => $selection_id]), 'attributes' => $attributes];
$row['operations'] = ['#type' => 'operations', '#links' => $operations];
$form['selection_section']['table'][$selection_id] = $row;
}
}
}
return $form;
}
示例6: submitForm
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state)
{
$form_state->cleanValues();
// This won't have a proper JSON header, but Drupal doesn't check for that
// anyway so this is fine until it's replaced with a JsonResponse.
print Json::encode($form_state->getValues());
exit;
}
示例7: submitForm
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state)
{
form_state_values_clean($form_state);
// This won't have a proper JSON header, but Drupal doesn't check for that
// anyway so this is fine until it's replaced with a JsonResponse.
print Json::encode($form_state['values']);
exit;
}
示例8: render
/**
* {@inheritdoc}
*/
public function render(array $js_assets)
{
$elements = array();
// A dummy query-string is added to filenames, to gain control over
// browser-caching. The string changes on every update or full cache
// flush, forcing browsers to load a new copy of the files, as the
// URL changed. Files that should not be cached (see _drupal_add_js())
// get REQUEST_TIME as query-string instead, to enforce reload on every
// page request.
$default_query_string = $this->state->get('system.css_js_query_string') ?: '0';
// For inline JavaScript to validate as XHTML, all JavaScript containing
// XHTML needs to be wrapped in CDATA. To make that backwards compatible
// with HTML 4, we need to comment out the CDATA-tag.
$embed_prefix = "\n<!--//--><![CDATA[//><!--\n";
$embed_suffix = "\n//--><!]]>\n";
// Defaults for each SCRIPT element.
$element_defaults = array('#type' => 'html_tag', '#tag' => 'script', '#value' => '');
// Loop through all JS assets.
foreach ($js_assets as $js_asset) {
// Element properties that do not depend on JS asset type.
$element = $element_defaults;
$element['#browsers'] = $js_asset['browsers'];
// Element properties that depend on item type.
switch ($js_asset['type']) {
case 'setting':
$element['#value_prefix'] = $embed_prefix;
$element['#value'] = 'var drupalSettings = ' . Json::encode(drupal_merge_js_settings($js_asset['data'])) . ";";
$element['#value_suffix'] = $embed_suffix;
break;
case 'inline':
$element['#value_prefix'] = $embed_prefix;
$element['#value'] = $js_asset['data'];
$element['#value_suffix'] = $embed_suffix;
break;
case 'file':
$query_string = empty($js_asset['version']) ? $default_query_string : 'v=' . $js_asset['version'];
$query_string_separator = strpos($js_asset['data'], '?') !== FALSE ? '&' : '?';
$element['#attributes']['src'] = file_create_url($js_asset['data']);
// Only add the cache-busting query string if this isn't an aggregate
// file.
if (!isset($js_asset['preprocessed'])) {
$element['#attributes']['src'] .= $query_string_separator . ($js_asset['cache'] ? $query_string : REQUEST_TIME);
}
break;
case 'external':
$element['#attributes']['src'] = $js_asset['data'];
break;
default:
throw new \Exception('Invalid JS asset type.');
}
// Attributes may only be set if this script is output independently.
if (!empty($element['#attributes']['src']) && !empty($js_asset['attributes'])) {
$element['#attributes'] += $js_asset['attributes'];
}
$elements[] = $element;
}
return $elements;
}
示例9: form
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state)
{
$form = parent::form($form, $form_state);
$form['use_admin_theme'] = ['#type' => 'checkbox', '#title' => $this->t('Use admin theme'), '#default_value' => $this->entity->usesAdminTheme()];
$attributes = ['class' => ['use-ajax'], 'data-dialog-type' => 'modal', 'data-dialog-options' => Json::encode(['width' => 'auto'])];
$add_button_attributes = NestedArray::mergeDeep($attributes, ['class' => ['button', 'button--small', 'button-action']]);
$form['context'] = ['#type' => 'details', '#title' => $this->t('Available context'), '#open' => TRUE];
$form['context']['add'] = ['#type' => 'link', '#title' => $this->t('Add new static context'), '#url' => Url::fromRoute('page_manager.static_context_add', ['page' => $this->entity->id()]), '#attributes' => $add_button_attributes, '#attached' => ['library' => ['core/drupal.ajax']]];
$form['context']['available_context'] = ['#type' => 'table', '#header' => [$this->t('Label'), $this->t('Name'), $this->t('Type'), $this->t('Operations')], '#empty' => $this->t('There is no available context.')];
$contexts = $this->entity->getContexts();
foreach ($contexts as $name => $context) {
$context_definition = $context->getContextDefinition();
$row = [];
$row['label'] = ['#markup' => $context_definition->getLabel()];
$row['machine_name'] = ['#markup' => $name];
$row['type'] = ['#markup' => $context_definition->getDataType()];
// Add operation links if the context is a static context.
$operations = [];
if ($this->entity->getStaticContext($name)) {
$operations['edit'] = ['title' => $this->t('Edit'), 'url' => Url::fromRoute('page_manager.static_context_edit', ['page' => $this->entity->id(), 'name' => $name]), 'attributes' => $attributes];
$operations['delete'] = ['title' => $this->t('Delete'), 'url' => Url::fromRoute('page_manager.static_context_delete', ['page' => $this->entity->id(), 'name' => $name]), 'attributes' => $attributes];
}
$row['operations'] = ['#type' => 'operations', '#links' => $operations];
$form['context']['available_context'][$name] = $row;
}
$form['display_variant_section'] = ['#type' => 'details', '#title' => $this->t('Display variants'), '#open' => TRUE];
$form['display_variant_section']['add_new_page'] = ['#type' => 'link', '#title' => $this->t('Add new display variant'), '#url' => Url::fromRoute('page_manager.display_variant_select', ['page' => $this->entity->id()]), '#attributes' => $add_button_attributes, '#attached' => ['library' => ['core/drupal.ajax']]];
$form['display_variant_section']['display_variants'] = ['#type' => 'table', '#header' => [$this->t('Label'), $this->t('Plugin'), $this->t('Weight'), $this->t('Operations')], '#empty' => $this->t('There are no display variants.'), '#tabledrag' => [['action' => 'order', 'relationship' => 'sibling', 'group' => 'display-variant-weight']]];
foreach ($this->entity->getVariants() as $display_variant_id => $display_variant) {
$row = ['#attributes' => ['class' => ['draggable']]];
$row['label']['#markup'] = $display_variant->label();
$row['id']['#markup'] = $display_variant->adminLabel();
$row['weight'] = ['#type' => 'weight', '#default_value' => $display_variant->getWeight(), '#title' => $this->t('Weight for @display_variant display variant', ['@display_variant' => $display_variant->label()]), '#title_display' => 'invisible', '#attributes' => ['class' => ['display-variant-weight']]];
$operations = [];
$operations['edit'] = ['title' => $this->t('Edit'), 'url' => Url::fromRoute('page_manager.display_variant_edit', ['page' => $this->entity->id(), 'display_variant_id' => $display_variant_id])];
$operations['delete'] = ['title' => $this->t('Delete'), 'url' => Url::fromRoute('page_manager.display_variant_delete', ['page' => $this->entity->id(), 'display_variant_id' => $display_variant_id])];
$row['operations'] = ['#type' => 'operations', '#links' => $operations];
$form['display_variant_section']['display_variants'][$display_variant_id] = $row;
}
if ($access_conditions = $this->entity->getAccessConditions()) {
$form['access_section_section'] = ['#type' => 'details', '#title' => $this->t('Access Conditions'), '#open' => TRUE];
$form['access_section_section']['add'] = ['#type' => 'link', '#title' => $this->t('Add new access condition'), '#url' => Url::fromRoute('page_manager.access_condition_select', ['page' => $this->entity->id()]), '#attributes' => $add_button_attributes, '#attached' => ['library' => ['core/drupal.ajax']]];
$form['access_section_section']['access_section'] = ['#type' => 'table', '#header' => [$this->t('Label'), $this->t('Description'), $this->t('Operations')], '#empty' => $this->t('There are no access conditions.')];
$form['access_section_section']['access_logic'] = ['#type' => 'radios', '#options' => ['and' => $this->t('All conditions must pass'), 'or' => $this->t('Only one condition must pass')], '#default_value' => $this->entity->getAccessLogic()];
$form['access_section_section']['access'] = ['#tree' => TRUE];
foreach ($access_conditions as $access_id => $access_condition) {
$row = [];
$row['label']['#markup'] = $access_condition->getPluginDefinition()['label'];
$row['description']['#markup'] = $access_condition->summary();
$operations = [];
$operations['edit'] = ['title' => $this->t('Edit'), 'url' => Url::fromRoute('page_manager.access_condition_edit', ['page' => $this->entity->id(), 'condition_id' => $access_id]), 'attributes' => $attributes];
$operations['delete'] = ['title' => $this->t('Delete'), 'url' => Url::fromRoute('page_manager.access_condition_delete', ['page' => $this->entity->id(), 'condition_id' => $access_id]), 'attributes' => $attributes];
$row['operations'] = ['#type' => 'operations', '#links' => $operations];
$form['access_section_section']['access_section'][$access_id] = $row;
}
}
return $form;
}
示例10: linksDisplay
/**
* Displays test links that will open in offcanvas tray.
*
* @return array
* Render array with links.
*/
public function linksDisplay() {
return [
'offcanvas_link_1' => [
'#title' => 'Click Me 1!',
'#type' => 'link',
'#url' => Url::fromRoute('offcanvas_test.thing1'),
'#attributes' => [
'class' => ['use-ajax'],
'data-dialog-type' => 'dialog',
'data-dialog-renderer' => 'offcanvas',
],
'#attached' => [
'library' => [
'outside_in/drupal.outside_in',
],
],
],
'offcanvas_link_2' => [
'#title' => 'Click Me 2!',
'#type' => 'link',
'#url' => Url::fromRoute('offcanvas_test.thing2'),
'#attributes' => [
'class' => ['use-ajax'],
'data-dialog-type' => 'dialog',
'data-dialog-renderer' => 'offcanvas',
'data-dialog-options' => Json::encode([
'width' => 555,
]),
],
'#attached' => [
'library' => [
'outside_in/drupal.outside_in',
],
],
],
'other_dialog_links' => [
'#title' => 'Display more links!',
'#type' => 'link',
'#url' => Url::fromRoute('offcanvas_test.dialog_links'),
'#attributes' => [
'class' => ['use-ajax'],
'data-dialog-type' => 'dialog',
'data-dialog-renderer' => 'offcanvas',
],
'#attached' => [
'library' => [
'outside_in/drupal.outside_in',
],
],
],
];
}
示例11: submitForm
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state)
{
$main_config = $this->configFactory->get('nodejs.config');
$ext = $form_state->getValue('nodejs_config_extensions');
if ($ext == !NULL) {
$ext = explode("\n", str_replace("\r", '', $ext));
}
$array = array('scheme' => $form_state->getValue('nodejs_server_scheme'), 'host' => $form_state->getValue('nodejs_config_host'), 'port' => (int) $form_state->getValue('nodejs_config_port'), 'key' => $form_state->getValue('nodejs_config_key'), 'cert' => $form_state->getValue('nodejs_config_cert'), 'resource' => $form_state->getValue('nodejs_config_resource'), 'publishUrl' => $form_state->getValue('nodejs_config_publish_url'), 'serviceKey' => $main_config->get('nodejs_service_key'), 'backend' => array('port' => (int) $form_state->getValue('nodejs_config_backend_port'), 'host' => $form_state->getValue('nodejs_config_backend_host'), 'messagePath' => $form_state->getValue('nodejs_config_backend_message_path')), 'clientsCanWriteToChannels' => (bool) $form_state->getValue('nodejs_config_write_channels'), 'clientsCanWriteToClients' => (bool) $form_state->getValue('nodejs_config_write_clients'), 'extensions' => $ext, 'debug' => (bool) $form_state->getValue('nodejs_config_debug'), 'transports' => array('websocket', 'flashsocket', 'htmlfile', 'xhr-polling', 'jsonp-polling'), 'jsMinification' => true, 'jsEtag' => true, 'logLevel' => 1);
$output = 'backendSettings = ' . Json::encode($array);
$output = str_replace(array('= {', ',', '}}', ':{', '\\/'), array("= {\n ", ",\n ", "\n }\n}", ":{\n ", '/'), $output);
$output = "/**\n* This configuration file was built using the 'Node.js server configuration builder'.\n* For a more fully commented example see the file nodejs.config.js.example in the root of this module\n*/\n" . $output . ';';
$this->configFactory->getEditable('nodejs_config.settings')->set('js_suggestion', $output)->set('host', $form_state->getValue('nodejs_config_host'))->set('port', (int) $form_state->getValue('nodejs_config_port'))->set('scheme', $form_state->getValue('nodejs_server_scheme'))->set('key', $form_state->getValue('nodejs_config_key'))->set('cert', $form_state->getValue('nodejs_config_cert'))->set('resource', $form_state->getValue('nodejs_config_resource'))->set('publish_url', $form_state->getValue('nodejs_config_publish_url'))->set('backend_port', (int) $form_state->getValue('nodejs_config_backend_port'))->set('backend_host', $form_state->getValue('nodejs_config_backend_host'))->set('backend_message_path', $form_state->getValue('nodejs_config_backend_message_path'))->set('write_channels', (bool) $form_state->getValue('nodejs_config_write_channels'))->set('write_clients', (bool) $form_state->getValue('nodejs_config_write_clients'))->set('debug', (bool) $form_state->getValue('nodejs_config_debug'))->set('extensions', $form_state->getValue('nodejs_config_extensions'))->save();
parent::submitForm($form, $form_state);
}
示例12: form
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state)
{
$form = parent::form($form, $form_state);
$menu_links_elements = $this->getMenuLinkElements($this->entity->getTargetMenu());
$layout_options = $this->getLayoutOptions();
$table_header = ['label' => $this->t('Label'), 'category' => $this->t('Category'), 'region' => $this->t('Region'), 'weight' => $this->t('Weight'), 'operations' => $this->t('Operations')];
$form['links'] = ['#type' => 'container', '#title' => $this->t('Menu links'), '#tree' => TRUE];
$blocks = $this->entity->getAllBlocksSortedByLink();
foreach ($menu_links_elements as $link_element_id => $link_element) {
// Replace any dots with a underscore since dots are not supported as
// keys in the configuration data.
$link_element_id = str_replace('.', '_', $link_element_id);
$link_layout = $this->entity->getLinkLayout($link_element_id);
$regions = $this->getLayoutRegions($link_layout);
$link_id = Html::getId($link_element_id);
$form['links'][$link_element_id] = ['#type' => 'fieldset', '#title' => $link_element->link->getTitle()];
$form['links'][$link_element_id]['layout'] = ['#type' => 'select', '#title' => $this->t('Layout'), '#options' => $layout_options, '#default_value' => $link_layout, '#ajax' => ['callback' => '::onLayoutSelect', 'wrapper' => "mega-menu-link-{$link_id}-blocks-wrapper"], '#attributes' => ['data-link-element-id' => $link_element_id]];
$form['links'][$link_element_id]['blocks'] = ['#prefix' => '<div id="mega-menu-link-' . $link_id . '-blocks-wrapper">', '#suffix' => '</div>', '#type' => 'table', '#header' => $table_header, '#empty' => $this->t('There are no regions for blocks.')];
// Add regions.
foreach ($regions as $region_key => $region_name) {
// Tabledrag stuff.
$form['links'][$link_element_id]['blocks']['#tabledrag'][] = ['action' => 'match', 'relationship' => 'sibling', 'group' => 'block-region-select', 'subgroup' => 'block-region-' . $region_key, 'hidden' => FALSE];
$form['links'][$link_element_id]['blocks']['#tabledrag'][] = ['action' => 'order', 'relationship' => 'sibling', 'group' => 'block-weight', 'subgroup' => 'block-weight-' . $region_key];
// Regions.
$form['links'][$link_element_id]['blocks'][$region_key] = ['#attributes' => ['class' => ['region-title', 'region-title-' . $region_key], 'no_striping' => TRUE]];
if ($region_key === MegaMenuInterface::NO_REGION) {
$form['links'][$link_element_id]['blocks'][$region_key]['title'] = ['#markup' => $region_name, '#wrapper_attributes' => ['colspan' => 5]];
} else {
$form['links'][$link_element_id]['blocks'][$region_key]['title'] = ['#theme_wrappers' => ['container' => ['#attributes' => ['class' => ['region-title__action']]]], '#prefix' => $region_name, '#type' => 'link', '#title' => $this->t('Place block <span class="visually-hidden">in the %region region</span>', ['%region' => $region_name]), '#url' => Url::fromRoute('mega_menu.block_library', ['mega_menu' => $this->entity->id()], ['query' => ['link' => $link_element_id, 'region' => $region_key]]), '#wrapper_attributes' => ['colspan' => 5], '#attributes' => ['class' => ['use-ajax', 'button', 'button--small'], 'data-dialog-type' => 'modal', 'data-dialog-options' => Json::encode(['width' => 700])]];
}
$blocks_by_region = isset($blocks[$link_element_id]) ? $blocks[$link_element_id]->getAllByRegion() : [];
$region_message_class = empty($blocks_by_region[$region_key]) ? 'region-empty' : 'region-populated';
$form['links'][$link_element_id]['blocks'][$region_key . '-message'] = ['#attributes' => ['class' => ['region-message', 'region-' . $region_key . '-message', $region_message_class]]];
$form['links'][$link_element_id]['blocks'][$region_key . '-message']['message'] = ['#markup' => '<em>' . $this->t('No blocks in this region') . '</em>', '#wrapper_attributes' => ['colspan' => 5]];
if (!isset($blocks_by_region[$region_key])) {
continue;
}
/** @var BlockPluginInterface $block */
foreach ($blocks_by_region[$region_key] as $block_id => $block) {
if (!isset($form['links'][$link_element_id])) {
continue;
}
$operations = ['edit' => ['title' => $this->t('Edit'), 'url' => Url::fromRoute('mega_menu.block_edit', ['mega_menu' => $this->entity->id(), 'block_id' => $block_id], ['query' => ['link' => $link_element_id, 'region' => $region_key]]), 'attributes' => ['class' => ['use-ajax', 'button', 'button--small'], 'data-dialog-type' => 'modal', 'data-dialog-options' => Json::encode(['width' => 700])]], 'delete' => ['title' => $this->t('Delete'), 'url' => Url::fromRoute('mega_menu.block_delete', ['mega_menu' => $this->entity->id(), 'block_id' => $block_id], ['query' => ['link' => $link_element_id]]), 'attributes' => ['class' => ['use-ajax', 'button', 'button--small'], 'data-dialog-type' => 'modal', 'data-dialog-options' => Json::encode(['width' => 700])]]];
$configuration = $block->getConfiguration();
$form['links'][$link_element_id]['blocks'][$block_id] = ['#attributes' => ['class' => ['draggable']], 'label' => ['#markup' => $block->label()], 'category' => ['#markup' => $block->getPluginDefinition()['category']], 'region' => ['#type' => 'select', '#title' => $this->t('Region for @block block', ['@block' => $block->label()]), '#title_display' => 'invisible', '#default_value' => $region_key, '#options' => $regions, '#attributes' => ['class' => ['block-region-select', 'block-region-' . $region_key]]], 'weight' => ['#type' => 'weight', '#default_value' => isset($configuration['weight']) ? $configuration['weight'] : 0, '#title' => $this->t('Weight for @block block', ['@block' => $block->label()]), '#title_display' => 'invisible', '#attributes' => ['class' => ['block-weight', 'block-weight-' . $configuration['region']]]], 'operations' => ['#type' => 'operations', '#links' => $operations]];
}
}
}
return $form;
}
示例13: selectAddress
/**
* Chooses an address to fill out a form.
*/
protected function selectAddress(array $addresses = [])
{
$quote_config = \Drupal::config('uc_quote.settings');
$store_address = $quote_config->get('store_default_address');
$store_address = new \Drupal\uc_store\Address();
if (!in_array($store_address, $addresses)) {
$addresses[] = $store_address;
}
$blank = array('first_name' => '', 'last_name' => '', 'phone' => '', 'company' => '', 'street1' => '', 'street2' => '', 'city' => '', 'postal_code' => '', 'country' => 0, 'zone' => 0);
$options = array(Json::encode($blank) => t('- Reset fields -'));
foreach ($addresses as $address) {
$options[Json::encode($address)] = $address->company . ' ' . $address->street1 . ' ' . $address->city;
}
$select = array('#type' => 'select', '#title' => t('Saved addresses'), '#options' => $options, '#default_value' => Json::encode($addresses[0]), '#attributes' => array('onchange' => 'apply_address(\'pickup\', this.value);'));
return $select;
}
示例14: generateFromUrl
/**
* {@inheritdoc}
*
* For anonymous users, the "active" class will be calculated on the server,
* because most sites serve each anonymous user the same cached page anyway.
* For authenticated users, the "active" class will be calculated on the
* client (through JavaScript), only data- attributes are added to links to
* prevent breaking the render cache. The JavaScript is added in
* system_page_build().
*
* @see system_page_build()
*/
public function generateFromUrl($text, Url $url)
{
// Start building a structured representation of our link to be altered later.
$variables = array('text' => is_array($text) ? drupal_render($text) : $text, 'url' => $url, 'options' => $url->getOptions());
// Merge in default options.
$variables['options'] += array('attributes' => array(), 'query' => array(), 'html' => FALSE, 'language' => NULL, 'set_active_class' => FALSE, 'absolute' => FALSE);
// Add a hreflang attribute if we know the language of this link's url and
// hreflang has not already been set.
if (!empty($variables['options']['language']) && !isset($variables['options']['attributes']['hreflang'])) {
$variables['options']['attributes']['hreflang'] = $variables['options']['language']->id;
}
// Set the "active" class if the 'set_active_class' option is not empty.
if (!empty($variables['options']['set_active_class']) && !$url->isExternal()) {
// Add a "data-drupal-link-query" attribute to let the
// drupal.active-link library know the query in a standardized manner.
if (!empty($variables['options']['query'])) {
$query = $variables['options']['query'];
ksort($query);
$variables['options']['attributes']['data-drupal-link-query'] = Json::encode($query);
}
// Add a "data-drupal-link-system-path" attribute to let the
// drupal.active-link library know the path in a standardized manner.
if (!isset($variables['options']['attributes']['data-drupal-link-system-path'])) {
// @todo System path is deprecated - use the route name and parameters.
$system_path = $url->getInternalPath();
// Special case for the front page.
$variables['options']['attributes']['data-drupal-link-system-path'] = $system_path == '' ? '<front>' : $system_path;
}
}
// Remove all HTML and PHP tags from a tooltip, calling expensive strip_tags()
// only when a quick strpos() gives suspicion tags are present.
if (isset($variables['options']['attributes']['title']) && strpos($variables['options']['attributes']['title'], '<') !== FALSE) {
$variables['options']['attributes']['title'] = strip_tags($variables['options']['attributes']['title']);
}
// Allow other modules to modify the structure of the link.
$this->moduleHandler->alter('link', $variables);
// Move attributes out of options. generateFromRoute(() doesn't need them.
$attributes = new Attribute($variables['options']['attributes']);
unset($variables['options']['attributes']);
$url->setOptions($variables['options']);
// The result of the url generator is a plain-text URL. Because we are using
// it here in an HTML argument context, we need to encode it properly.
$url = String::checkPlain($url->toString());
// Sanitize the link text if necessary.
$text = $variables['options']['html'] ? $variables['text'] : String::checkPlain($variables['text']);
return SafeMarkup::set('<a href="' . $url . '"' . $attributes . '>' . $text . '</a>');
}
示例15: render
/**
* {@inheritdoc}
*
* This class evaluates the aggregation enabled/disabled condition on a group
* by group basis by testing whether an aggregate file has been made for the
* group rather than by testing the site-wide aggregation setting. This allows
* this class to work correctly even if modules have implemented custom
* logic for grouping and aggregating files.
*/
public function render(array $js_assets)
{
$elements = array();
// A dummy query-string is added to filenames, to gain control over
// browser-caching. The string changes on every update or full cache
// flush, forcing browsers to load a new copy of the files, as the
// URL changed. Files that should not be cached get REQUEST_TIME as
// query-string instead, to enforce reload on every page request.
$default_query_string = $this->state->get('system.css_js_query_string') ?: '0';
// Defaults for each SCRIPT element.
$element_defaults = array('#type' => 'html_tag', '#tag' => 'script', '#value' => '');
// Loop through all JS assets.
foreach ($js_assets as $js_asset) {
// Element properties that do not depend on JS asset type.
$element = $element_defaults;
$element['#browsers'] = $js_asset['browsers'];
// Element properties that depend on item type.
switch ($js_asset['type']) {
case 'setting':
$element['#attributes'] = array('type' => 'application/json', 'data-drupal-selector' => 'drupal-settings-json');
$element['#value'] = Json::encode($js_asset['data']);
break;
case 'file':
$query_string = $js_asset['version'] == -1 ? $default_query_string : 'v=' . $js_asset['version'];
$query_string_separator = strpos($js_asset['data'], '?') !== FALSE ? '&' : '?';
$element['#attributes']['src'] = file_url_transform_relative(file_create_url($js_asset['data']));
// Only add the cache-busting query string if this isn't an aggregate
// file.
if (!isset($js_asset['preprocessed'])) {
$element['#attributes']['src'] .= $query_string_separator . ($js_asset['cache'] ? $query_string : REQUEST_TIME);
}
break;
case 'external':
$element['#attributes']['src'] = $js_asset['data'];
break;
default:
throw new \Exception('Invalid JS asset type.');
}
// Attributes may only be set if this script is output independently.
if (!empty($element['#attributes']['src']) && !empty($js_asset['attributes'])) {
$element['#attributes'] += $js_asset['attributes'];
}
$elements[] = $element;
}
return $elements;
}