Esempio n. 1
0
 protected function _split($lines)
 {
     $words = array();
     $stripped = array();
     $first = TRUE;
     foreach ($lines as $line) {
         // If the line is too long, just pretend the entire line is one big word
         // This prevents resource exhaustion problems
         if ($first) {
             $first = FALSE;
         } else {
             $words[] = "\n";
             $stripped[] = "\n";
         }
         if (Unicode::strlen($line) > $this::MAX_LINE_LENGTH) {
             $words[] = $line;
             $stripped[] = $line;
         } else {
             if (preg_match_all('/ ( [^\\S\\n]+ | [0-9_A-Za-z\\x80-\\xff]+ | . ) (?: (?!< \\n) [^\\S\\n])? /xs', $line, $m)) {
                 $words = array_merge($words, $m[0]);
                 $stripped = array_merge($stripped, $m[1]);
             }
         }
     }
     return array($words, $stripped);
 }
Esempio n. 2
0
 /**
  * Validates whether a hexadecimal color value is syntatically correct.
  *
  * @param $hex
  *   The hexadecimal string to validate. May contain a leading '#'. May use
  *   the shorthand notation (e.g., '123' for '112233').
  *
  * @return bool
  *   TRUE if $hex is valid or FALSE if it is not.
  */
 public static function validateHex($hex)
 {
     // Must be a string.
     $valid = is_string($hex);
     // Hash prefix is optional.
     $hex = ltrim($hex, '#');
     // Must be either RGB or RRGGBB.
     $length = Unicode::strlen($hex);
     $valid = $valid && ($length === 3 || $length === 6);
     // Must be a valid hex value.
     $valid = $valid && ctype_xdigit($hex);
     return $valid;
 }
Esempio n. 3
0
 /**
  * Tests that all Unicode characters simplify correctly.
  */
 function testSearchSimplifyUnicode()
 {
     // This test uses a file that was constructed so that the even lines are
     // boundary characters, and the odd lines are valid word characters. (It
     // was generated as a sequence of all the Unicode characters, and then the
     // boundary characters (punctuation, spaces, etc.) were split off into
     // their own lines).  So the even-numbered lines should simplify to nothing,
     // and the odd-numbered lines we need to split into shorter chunks and
     // verify that simplification doesn't lose any characters.
     $input = file_get_contents(\Drupal::root() . '/core/modules/search/tests/UnicodeTest.txt');
     $basestrings = explode(chr(10), $input);
     $strings = array();
     foreach ($basestrings as $key => $string) {
         if ($key % 2) {
             // Even line - should simplify down to a space.
             $simplified = search_simplify($string);
             $this->assertIdentical($simplified, ' ', "Line {$key} is excluded from the index");
         } else {
             // Odd line, should be word characters.
             // Split this into 30-character chunks, so we don't run into limits
             // of truncation in search_simplify().
             $start = 0;
             while ($start < Unicode::strlen($string)) {
                 $newstr = Unicode::substr($string, $start, 30);
                 // Special case: leading zeros are removed from numeric strings,
                 // and there's one string in this file that is numbers starting with
                 // zero, so prepend a 1 on that string.
                 if (preg_match('/^[0-9]+$/', $newstr)) {
                     $newstr = '1' . $newstr;
                 }
                 $strings[] = $newstr;
                 $start += 30;
             }
         }
     }
     foreach ($strings as $key => $string) {
         $simplified = search_simplify($string);
         $this->assertTrue(Unicode::strlen($simplified) >= Unicode::strlen($string), "Nothing is removed from string {$key}.");
     }
     // Test the low-numbered ASCII control characters separately. They are not
     // in the text file because they are problematic for diff, especially \0.
     $string = '';
     for ($i = 0; $i < 32; $i++) {
         $string .= chr($i);
     }
     $this->assertIdentical(' ', search_simplify($string), 'Search simplify works for ASCII control characters.');
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public function uniquify(&$alias, $source, $langcode)
 {
     $config = $this->configFactory->get('pathauto.settings');
     if (!$this->isReserved($alias, $source, $langcode)) {
         return;
     }
     // If the alias already exists, generate a new, hopefully unique, variant.
     $maxlength = min($config->get('max_length'), $this->aliasStorageHelper->getAliasSchemaMaxlength());
     $separator = $config->get('separator');
     $original_alias = $alias;
     $i = 0;
     do {
         // Append an incrementing numeric suffix until we find a unique alias.
         $unique_suffix = $separator . $i;
         $alias = Unicode::truncate($original_alias, $maxlength - Unicode::strlen($unique_suffix, TRUE)) . $unique_suffix;
         $i++;
     } while ($this->isReserved($alias, $source, $langcode));
 }
Esempio n. 5
0
 /**
  * {@inheritdoc}
  */
 public function process($text, $langcode)
 {
     $result = new FilterProcessResult($text);
     if (stristr($text, 'data-caption') !== FALSE) {
         $dom = Html::load($text);
         $xpath = new \DOMXPath($dom);
         foreach ($xpath->query('//*[@data-caption]') as $node) {
             // Read the data-caption attribute's value, then delete it.
             $caption = Html::escape($node->getAttribute('data-caption'));
             $node->removeAttribute('data-caption');
             // Sanitize caption: decode HTML encoding, limit allowed HTML tags; only
             // allow inline tags that are allowed by default, plus <br>.
             $caption = Html::decodeEntities($caption);
             $caption = FilteredMarkup::create(Xss::filter($caption, array('a', 'em', 'strong', 'cite', 'code', 'br')));
             // The caption must be non-empty.
             if (Unicode::strlen($caption) === 0) {
                 continue;
             }
             // Given the updated node and caption: re-render it with a caption, but
             // bubble up the value of the class attribute of the captioned element,
             // this allows it to collaborate with e.g. the filter_align filter.
             $tag = $node->tagName;
             $classes = $node->getAttribute('class');
             $node->removeAttribute('class');
             $node = $node->parentNode->tagName === 'a' ? $node->parentNode : $node;
             $filter_caption = array('#theme' => 'filter_caption', '#node' => FilteredMarkup::create($node->C14N()), '#tag' => $tag, '#caption' => $caption, '#classes' => $classes);
             $altered_html = drupal_render($filter_caption);
             // Load the altered HTML into a new DOMDocument and retrieve the element.
             $updated_nodes = Html::load($altered_html)->getElementsByTagName('body')->item(0)->childNodes;
             foreach ($updated_nodes as $updated_node) {
                 // Import the updated node from the new DOMDocument into the original
                 // one, importing also the child nodes of the updated node.
                 $updated_node = $dom->importNode($updated_node, TRUE);
                 $node->parentNode->insertBefore($updated_node, $node);
             }
             // Finally, remove the original data-caption node.
             $node->parentNode->removeChild($node);
         }
         $result->setProcessedText(Html::serialize($dom))->addAttachments(array('library' => array('filter/caption')));
     }
     return $result;
 }
Esempio n. 6
0
 /**
  * {@inheritdoc}
  */
 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
 {
     module_load_include('inc', 'name', 'includes/name.content');
     $field_settings = $this->getFieldSettings();
     $instance['label'] = 'instance label';
     $element += array('#type' => 'name', '#title' => SafeMarkup::checkPlain($instance['label']), '#label' => $instance['label'], '#components' => array(), '#minimum_components' => array_filter($field_settings['minimum_components']), '#allow_family_or_given' => !empty($field_settings['allow_family_or_given']), '#default_value' => isset($items[$delta]) ? $items[$delta]->getValue() : NULL, '#field' => $this, '#credentials_inline' => empty($field_settings['credentials_inline']) ? 0 : 1, '#component_css' => empty($field_settings['component_css']) ? '' : $field_settings['component_css'], '#component_layout' => empty($field_settings['component_layout']) ? 'default' : $field_settings['component_layout'], '#show_component_required_marker' => !empty($field_settings['show_component_required_marker']));
     $components = array_filter($field_settings['components']);
     foreach (_name_translations() as $key => $title) {
         if (isset($components[$key])) {
             $element['#components'][$key]['type'] = 'textfield';
             $size = !empty($field_settings['size'][$key]) ? $field_settings['size'][$key] : 60;
             $title_display = isset($field_settings['title_display'][$key]) ? $field_settings['title_display'][$key] : 'description';
             $element['#components'][$key]['title'] = SafeMarkup::checkPlain($field_settings['labels'][$key]);
             $element['#components'][$key]['title_display'] = $title_display;
             $element['#components'][$key]['size'] = $size;
             $element['#components'][$key]['maxlength'] = !empty($field_settings['max_length'][$key]) ? $field_settings['max_length'][$key] : 255;
             // Provides backwards compatibility with Drupal 6 modules.
             $field_type = $key == 'title' || $key == 'generational' ? 'select' : 'text';
             $field_type = isset($field_settings['field_type'][$key]) ? $field_settings['field_type'][$key] : (isset($field_settings[$key . '_field']) ? $field_settings[$key . '_field'] : $field_type);
             if ($field_type == 'select') {
                 $element['#components'][$key]['type'] = 'select';
                 $element['#components'][$key]['size'] = 1;
                 $element['#components'][$key]['options'] = $this->optionsProvider->getOptions($this->fieldDefinition, $key);
             } elseif ($field_type == 'autocomplete') {
                 if ($sources = $field_settings['autocomplete_source'][$key]) {
                     $sources = array_filter($sources);
                     if (!empty($sources)) {
                         $element['#components'][$key]['autocomplete'] = array('#autocomplete_route_name' => 'name.autocomplete', '#autocomplete_route_parameters' => array('field_name' => $this->fieldDefinition->getName(), 'entity_type' => $this->fieldDefinition->getTargetEntityTypeId(), 'bundle' => $this->fieldDefinition->getTargetBundle(), 'component' => $key));
                     }
                 }
             }
             if (isset($field_settings['inline_css'][$key]) && Unicode::strlen($field_settings['inline_css'][$key])) {
                 $element['#components'][$key]['attributes'] = array('style' => $field_settings['inline_css'][$key]);
             }
         } else {
             $element['#components'][$key]['exclude'] = TRUE;
         }
     }
     return $element;
 }
 public function getOptions(FieldDefinitionInterface $field, $component)
 {
     $fs = $field->getFieldStorageDefinition()->getSettings();
     $options = $fs[$component . '_options'];
     foreach ($options as $index => $opt) {
         if (preg_match('/^\\[vocabulary:([0-9a-z\\_]{1,})\\]/', trim($opt), $matches)) {
             unset($options[$index]);
             if ($this->termStorage && $this->vocabularyStorage) {
                 $vocabulary = $this->vocabularyStorage->load($matches[1]);
                 if ($vocabulary) {
                     $max_length = isset($fs['max_length'][$component]) ? $fs['max_length'][$component] : 255;
                     foreach ($this->termStorage->loadTree($vocabulary->id()) as $term) {
                         if (Unicode::strlen($term->name) <= $max_length) {
                             $options[] = $term->name;
                         }
                     }
                 }
             }
         }
     }
     // Options could come from multiple sources, filter duplicates.
     $options = array_unique($options);
     if (isset($fs['sort_options']) && !empty($fs['sort_options'][$component])) {
         natcasesort($options);
     }
     $default = FALSE;
     foreach ($options as $index => $opt) {
         if (strpos($opt, '--') === 0) {
             unset($options[$index]);
             $default = trim(Unicode::substr($opt, 2));
         }
     }
     $options = array_map('trim', $options);
     $options = array_combine($options, $options);
     if ($default !== FALSE) {
         $options = array('' => $default) + $options;
     }
     return $options;
 }
 /**
  * {@inheritdoc}
  */
 public function validate($items, Constraint $constraint)
 {
     if (!isset($items) || !$items->value) {
         $this->context->addViolation($constraint->emptyMessage);
         return;
     }
     $name = $items->first()->value;
     if (substr($name, 0, 1) == ' ') {
         $this->context->addViolation($constraint->spaceBeginMessage);
     }
     if (substr($name, -1) == ' ') {
         $this->context->addViolation($constraint->spaceEndMessage);
     }
     if (strpos($name, '  ') !== FALSE) {
         $this->context->addViolation($constraint->multipleSpacesMessage);
     }
     if (preg_match('/[^\\x{80}-\\x{F7} a-z0-9@_.\'-]/i', $name) || preg_match('/[\\x{80}-\\x{A0}' . '\\x{AD}' . '\\x{2000}-\\x{200F}' . '\\x{2028}-\\x{202F}' . '\\x{205F}-\\x{206F}' . '\\x{FEFF}' . '\\x{FF01}-\\x{FF60}' . '\\x{FFF9}-\\x{FFFD}' . '\\x{0}-\\x{1F}]/u', $name)) {
         $this->context->addViolation($constraint->illegalMessage);
     }
     if (Unicode::strlen($name) > USERNAME_MAX_LENGTH) {
         $this->context->addViolation($constraint->tooLongMessage, array('%name' => $name, '%max' => USERNAME_MAX_LENGTH));
     }
 }
Esempio n. 9
0
 /**
  * {@inheritdoc}
  */
 public function validateForm(array &$form, FormStateInterface $form_state)
 {
     $name = $form_state->getValue('name');
     if (Unicode::strlen($name) > 64) {
         $form_state->setErrorByName('name', $this->t('The name cannot be longer than 64 characters.'));
     }
     $phone = $form_state->getValue('phone');
     if (Unicode::strlen($phone) > 64) {
         $form_state->setErrorByName('phone', $this->t('The phone cannot be longer than 64 characters.'));
     } else {
         // Validate phone uniqueness amongst all (other) entries.
         $query = $this->connection->select('phonebook', 'p')->fields('p', ['pbid']);
         $query->condition('phone', $phone);
         if ($pbid = $form_state->getValue('pbid')) {
             $query->condition('pbid', $pbid, '<>');
         }
         $result = $query->execute()->fetchObject();
         if ($result) {
             $form_state->setErrorByName('phone', $this->t('The phone must be unique.'));
         }
     }
     parent::validateForm($form, $form_state);
 }
 /**
  * Replaces URLs with absolute URLs.
  */
 public static function absoluteMailUrls($match)
 {
     global $base_url, $base_path;
     $regexp =& drupal_static(__FUNCTION__);
     $url = $label = '';
     if ($match) {
         if (empty($regexp)) {
             $regexp = '@^' . preg_quote($base_path, '@') . '@';
         }
         list(, $url, $label) = $match;
         $url = strpos($url, '://') ? $url : preg_replace($regexp, $base_url . '/', $url);
         // If the link is formed by Drupal's URL filter, we only return the URL.
         // The URL filter generates a label out of the original URL.
         if (strpos($label, '...') === Unicode::strlen($label) - 3) {
             // Remove ellipsis from end of label.
             $label = Unicode::substr($label, 0, Unicode::strlen($label) - 3);
         }
         if (strpos($url, $label) !== FALSE) {
             return $url;
         }
         return $label . ' ' . $url;
     }
 }
 /**
  * {@inheritdoc}
  */
 public function encrypt($key, $source, $sourcelen = 0)
 {
     $this->errors = array();
     // Convert key into sequence of numbers
     $fudgefactor = $this->convertKey($key);
     if ($this->errors) {
         return;
     }
     if (empty($source)) {
         // Commented out to prevent errors getting logged for use cases that may
         // have variable encryption/decryption requirements. -RS
         // $this->errors[] = t('No value has been supplied for encryption');
         return;
     }
     while (strlen($source) < $sourcelen) {
         $source .= ' ';
     }
     $target = NULL;
     $factor2 = 0;
     for ($i = 0; $i < Unicode::strlen($source); $i++) {
         $char1 = Unicode::substr($source, $i, 1);
         $num1 = strpos(self::$scramble1, $char1);
         if ($num1 === FALSE) {
             $this->errors[] = t('Source string contains an invalid character (@char)', ['@char' => $char1]);
             return;
         }
         $adj = $this->applyFudgeFactor($fudgefactor);
         $factor1 = $factor2 + $adj;
         $num2 = round($factor1) + $num1;
         $num2 = $this->checkRange($num2);
         $factor2 = $factor1 + $num2;
         $char2 = substr(self::$scramble2, $num2, 1);
         $target .= $char2;
     }
     return $target;
 }
 /**
  * {@inheritdoc}
  */
 public function process($text, $langcode)
 {
     $result = new FilterProcessResult($text);
     if (stristr($text, '<img ') !== FALSE) {
         $dom = Html::load($text);
         $images = $dom->getElementsByTagName('img');
         foreach ($images as $image) {
             $src = $image->getAttribute("src");
             // The src must be non-empty.
             if (Unicode::strlen($src) === 0) {
                 continue;
             }
             // The src must not already be an external URL
             if (stristr($src, 'http://') !== FALSE || stristr($src, 'https://') !== FALSE) {
                 continue;
             }
             $url = Url::fromUri('internal:' . $src, array('absolute' => TRUE));
             $url_string = $url->toString();
             $image->setAttribute('src', $url_string);
         }
         $result->setProcessedText(Html::serialize($dom));
     }
     return $result;
 }
 /**
  * Retrieves suggestions for block category autocompletion.
  *
  * @param \Symfony\Component\HttpFoundation\Request $request
  *   The current request.
  * @param string $token_type
  *   The token type.
  * @param string $filter
  *   The autocomplete filter.
  *
  * @return \Symfony\Component\HttpFoundation\JsonResponse
  *   A JSON response containing autocomplete suggestions.
  */
 public function autocomplete($token_type, $filter, Request $request)
 {
     $filter = substr($filter, strrpos($filter, '['));
     $matches = array();
     if (!Unicode::strlen($filter)) {
         $matches["[{$token_type}:"] = 0;
     } else {
         $depth = max(1, substr_count($filter, ':'));
         $tree = $this->treeBuilder->buildTree($token_type, ['flat' => TRUE, 'depth' => $depth]);
         foreach (array_keys($tree) as $token) {
             if (strpos($token, $filter) === 0) {
                 $matches[$token] = levenshtein($token, $filter);
                 if (isset($tree[$token]['children'])) {
                     $token = rtrim($token, ':]') . ':';
                     $matches[$token] = levenshtein($token, $filter);
                 }
             }
         }
     }
     asort($matches);
     $keys = array_keys($matches);
     $matches = array_combine($keys, $keys);
     return new JsonResponse($matches);
 }
 /**
  * Returns the whole line if it's small enough, or the MD5 hash otherwise.
  */
 protected function _line_hash($line)
 {
     if (Unicode::strlen($line) > $this::MAX_XREF_LENGTH) {
         return md5($line);
     } else {
         return $line;
     }
 }
 /**
  * Constructs a new EntityType.
  *
  * @param array $definition
  *   An array of values from the annotation.
  *
  * @throws \Drupal\Core\Entity\Exception\EntityTypeIdLengthException
  *   Thrown when attempting to instantiate an entity type with too long ID.
  */
 public function __construct($definition)
 {
     // Throw an exception if the entity type ID is longer than 32 characters.
     if (Unicode::strlen($definition['id']) > static::ID_MAX_LENGTH) {
         throw new EntityTypeIdLengthException(String::format('Attempt to create an entity type with an ID longer than @max characters: @id.', array('@max' => static::ID_MAX_LENGTH, '@id' => $definition['id'])));
     }
     foreach ($definition as $property => $value) {
         $this->{$property} = $value;
     }
     // Ensure defaults.
     $this->entity_keys += array('revision' => '', 'bundle' => '');
     $this->handlers += array('access' => 'Drupal\\Core\\Entity\\EntityAccessControlHandler');
     // Ensure a default list cache tag is set.
     if (empty($this->list_cache_tags)) {
         $this->list_cache_tags = [$definition['id'] . '_list'];
     }
 }
Esempio n. 16
0
 /**
  * Creates a custom menu.
  *
  * @return \Drupal\system\Entity\Menu
  *   The custom menu that has been created.
  */
 function addCustomMenu()
 {
     // Try adding a menu using a menu_name that is too long.
     $this->drupalGet('admin/structure/menu/add');
     $menu_name = substr(hash('sha256', $this->randomMachineName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI + 1);
     $label = $this->randomMachineName(16);
     $edit = array('id' => $menu_name, 'description' => '', 'label' => $label);
     $this->drupalPostForm('admin/structure/menu/add', $edit, t('Save'));
     // Verify that using a menu_name that is too long results in a validation
     // message.
     $this->assertRaw(t('@name cannot be longer than %max characters but is currently %length characters long.', array('@name' => t('Menu name'), '%max' => MENU_MAX_MENU_NAME_LENGTH_UI, '%length' => Unicode::strlen($menu_name))));
     // Change the menu_name so it no longer exceeds the maximum length.
     $menu_name = substr(hash('sha256', $this->randomMachineName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI);
     $edit['id'] = $menu_name;
     $this->drupalPostForm('admin/structure/menu/add', $edit, t('Save'));
     // Verify that no validation error is given for menu_name length.
     $this->assertNoRaw(t('@name cannot be longer than %max characters but is currently %length characters long.', array('@name' => t('Menu name'), '%max' => MENU_MAX_MENU_NAME_LENGTH_UI, '%length' => Unicode::strlen($menu_name))));
     // Verify that the confirmation message is displayed.
     $this->assertRaw(t('Menu %label has been added.', array('%label' => $label)));
     $this->drupalGet('admin/structure/menu');
     $this->assertText($label, 'Menu created');
     // Confirm that the custom menu block is available.
     $this->drupalGet('admin/structure/block/list/' . $this->config('system.theme')->get('default'));
     $this->clickLinkPartialName('Place block');
     $this->assertText($label);
     // Enable the block.
     $block = $this->drupalPlaceBlock('system_menu_block:' . $menu_name);
     $this->blockPlacements[$menu_name] = $block->id();
     return Menu::load($menu_name);
 }
Esempio n. 17
0
 /**
  * {@inheritdoc}
  */
 protected static function validateAllowedValue($option)
 {
     if (Unicode::strlen($option) > 255) {
         return t('Allowed values list: each key must be a string at most 255 characters long.');
     }
 }
Esempio n. 18
0
 /**
  * Builds and returns the node information.
  *
  * @param bool $debug_mode
  *   The level of detail to include.
  *
  * @return array
  */
 public static function buildNodeInfo($debug_mode)
 {
     global $user;
     $visible_nodes = self::visibleNodes();
     if (count($visible_nodes) == 0) {
         return array();
     } else {
         $single_nid = reset($visible_nodes);
     }
     // Find out whether our DnaUser block is active or not.
     //dpm($blocks = \Drupal::entityTypeManager()->getStorage('block')->load());
     $user_block_active = FALSE;
     //foreach ($blocks as $block) {
     //  if ($block->get('plugin') == 'devel_dna_user_block') {
     //    $user_block_active = TRUE;
     //  }
     //}
     // Include rows where nid == 0.
     $nids = array_merge(array(0 => 0), $visible_nodes);
     $query = \Drupal::database()->select('node_access', 'na');
     $query->fields('na')->condition('na.nid', $nids, 'IN');
     $query->orderBy('na.nid')->orderBy('na.realm')->orderBy('na.gid');
     $nodes = Node::loadMultiple($nids);
     if (!$debug_mode) {
         $headers = array('node', 'realm', 'gid', 'view', 'update', 'delete', 'explained');
         $rows = array();
         foreach ($query->execute() as $row) {
             $explained = \Drupal::moduleHandler()->invokeAll('node_access_explain', [$row]);
             $node_title = self::get_node_title($nodes[$row->nid]);
             $title_attribute = \Drupal::request()->getRequestUri();
             if (Unicode::strlen($node_title) > 20) {
                 $title_attribute = $title_attribute . ': ' . $node_title;
                 $node_title = Unicode::substr($node_title, 0, 18) . '...';
             }
             $rows[] = array(empty($row->nid) ? '0' : Link::fromTextAndUrl($node_title, Url::fromUri(\Drupal::request()->getUri(), ['fragment' => 'node-' . $row->nid, 'attributes' => ['title' => $title_attribute]])), $row->realm, $row->gid, $row->grant_view, $row->grant_update, $row->grant_delete, implode('<br />', $explained));
         }
         $output[] = array('#theme' => 'table', '#header' => $headers, '#rows' => $rows, '#attributes' => array('style' => 'text-align: left'));
     } else {
         $tr = 't';
         $variables = array('!na' => '{node_access}');
         $states = array('default' => array(t('default'), 'ok', t('Default record supplied by core in the absence of any other non-empty records; in !na.', $variables)), 'ok' => array(t('ok'), 'ok', t('Highest priority record; in !na.', $variables)), 'removed' => array(t('removed'), '', t('Was removed in @func; not in !na.', $variables + array('@func' => 'hook_node_access_records_alter()'))), 'static' => array(t('static'), 'ok', t('Non-standard record in !na.', $variables)), 'unexpected' => array(t('unexpected'), 'warning', t('The 0/all/0/... record applies to all nodes and all users -- usually it should not be present in !na if any node access module is active!')), 'ignored' => array(t('ignored'), 'warning', t('Lower priority record; not in !na and thus ignored.', $variables)), 'empty' => array(t('empty'), 'warning', t('Does not grant any access, but could block lower priority records; not in !na.', $variables)), 'wrong' => array(t('wrong'), 'error', t('Is rightfully in !na but at least one access flag is wrong!', $variables)), 'missing' => array(t('missing'), 'error', t("Should be in !na but isn't!", $variables)), 'removed!' => array(t('removed!'), 'error', t('Was removed in @func; should NOT be in !na!', $variables + array('@func' => 'hook_node_access_records_alter()'))), 'illegitimate' => array(t('illegitimate'), 'error', t('Should NOT be in !na because of lower priority!', $variables)), 'alien' => array(t('alien'), 'error', t('Should NOT be in !na because of unknown origin!', $variables)));
         $active_states = array('default', 'ok', 'static', 'unexpected', 'wrong', 'illegitimate', 'alien');
         $headers = array(t('node'), t('prio'), t('status'), t('realm'), t('gid'), t('view'), t('update'), t('delete'), t('explained'));
         $headers = self::format_row($headers);
         $active_records = array();
         foreach ($query->execute() as $active_record) {
             $active_records[$active_record->nid][$active_record->realm][$active_record->gid] = $active_record;
         }
         $all_records = $grants_data = $checked_grants = $grants = array();
         foreach (array('view', 'update', 'delete') as $op) {
             $grants[$op] = self::simulate_module_invoke_all('node_grants', $user, $op);
             // Call all hook_node_grants_alter() implementations.
             $grants_data[$op] = self::simulate_node_grants_alter($grants[$op], $user, $op);
         }
         foreach ($nids as $nid) {
             $top_priority = -99999;
             $acquired_records_nid = array();
             if ($node = Node::load($nid)) {
                 // Check node_access_acquire_grants().
                 $records = self::simulate_module_invoke_all('node_access_records', $node);
                 // Check drupal_alter('node_access_records').
                 $data = self::simulate_node_access_records_alter($records, $node);
                 if (!empty($data)) {
                     foreach ($data as $data_by_realm) {
                         foreach ($data_by_realm as $data_by_realm_gid) {
                             if (isset($data_by_realm_gid['current'])) {
                                 $record = $data_by_realm_gid['current'];
                             } elseif (isset($data_by_realm_gid['original'])) {
                                 $record = $data_by_realm_gid['original'];
                                 $record['#removed'] = 1;
                             } else {
                                 continue;
                             }
                             $priority = intval(isset($record['priority']) ? $record['priority'] : 0);
                             $top_priority = isset($top_priority) ? max($top_priority, $priority) : $priority;
                             $record['priority'] = isset($record['priority']) ? $priority : '&ndash;&nbsp;';
                             $record['history'] = $data_by_realm_gid;
                             $acquired_records_nid[$priority][$record['realm']][$record['gid']] = $record + array('#title' => self::get_node_title($node), '#module' => isset($record['#module']) ? $record['#module'] : '');
                         }
                     }
                     krsort($acquired_records_nid);
                 }
                 //dpm($acquired_records_nid, "acquired_records_nid =");
                 // Check node_access_grants().
                 if ($node->id()) {
                     foreach (array('view', 'update', 'delete') as $op) {
                         $checked_grants[$nid][$op] = array_merge(array('all' => array(0)), $grants[$op]);
                     }
                 }
             }
             // Check for records in the node_access table that aren't returned by
             // node_access_acquire_grants().
             if (isset($active_records[$nid])) {
                 foreach ($active_records[$nid] as $realm => $active_records_realm) {
                     foreach ($active_records_realm as $gid => $active_record) {
                         $found = FALSE;
                         $count_nonempty_records = 0;
                         foreach ($acquired_records_nid as $priority => $acquired_records_nid_priority) {
                             if (isset($acquired_records_nid_priority[$realm][$gid])) {
                                 $found = TRUE;
                             }
                         }
                         // Take the highest priority only.
                         // TODO This has changed in D8!
                         if ($acquired_records_nid_priority = reset($acquired_records_nid)) {
                             foreach ($acquired_records_nid_priority as $acquired_records_nid_priority_realm) {
                                 foreach ($acquired_records_nid_priority_realm as $acquired_records_nid_priority_realm_gid) {
                                     $count_nonempty_records += !empty($acquired_records_nid_priority_realm_gid['grant_view']) || !empty($acquired_records_nid_priority_realm_gid['grant_update']) || !empty($acquired_records_nid_priority_realm_gid['grant_delete']);
                                 }
                             }
                         }
                         $fixed_record = (array) $active_record;
                         if ($count_nonempty_records == 0 && $realm == 'all' && $gid == 0) {
                             $fixed_record += array('priority' => '&ndash;', 'state' => 'default');
                         } elseif (!$found) {
                             $acknowledged = self::simulate_module_invoke_all('node_access_acknowledge', $fixed_record);
                             if (empty($acknowledged)) {
                                 // No module acknowledged this record, mark it as alien.
                                 $fixed_record += array('priority' => '?', 'state' => 'alien');
                             } else {
                                 // At least one module acknowledged the record,
                                 // attribute it to the first one.
                                 $fixed_record += array('priority' => '&ndash;', 'state' => 'static', '#module' => reset(array_keys($acknowledged)));
                             }
                         } else {
                             continue;
                         }
                         $fixed_record += array('nid' => $nid, '#title' => self::get_node_title($node));
                         $all_records[] = $fixed_record;
                     }
                 }
             }
             // Order records and evaluate their status.
             foreach ($acquired_records_nid as $priority => $acquired_records_priority) {
                 ksort($acquired_records_priority);
                 foreach ($acquired_records_priority as $realm => $acquired_records_realm) {
                     ksort($acquired_records_realm);
                     foreach ($acquired_records_realm as $gid => $acquired_record) {
                         // TODO: Handle priority.
                         //if ($priority == $top_priority) {
                         if (empty($acquired_record['grant_view']) && empty($acquired_record['grant_update']) && empty($acquired_record['grant_delete'])) {
                             $acquired_record['state'] = 'empty';
                         } else {
                             if (isset($active_records[$nid][$realm][$gid])) {
                                 $acquired_record['state'] = isset($acquired_record['#removed']) ? 'removed!' : 'ok';
                             } else {
                                 $acquired_record['state'] = isset($acquired_record['#removed']) ? 'removed' : 'missing';
                             }
                             if ($acquired_record['state'] == 'ok') {
                                 foreach (array('view', 'update', 'delete') as $op) {
                                     $active_record = (array) $active_records[$nid][$realm][$gid];
                                     if (empty($acquired_record["grant_{$op}"]) != empty($active_record["grant_{$op}"])) {
                                         $acquired_record["grant_{$op}!"] = $active_record["grant_{$op}"];
                                     }
                                 }
                             }
                         }
                         //}
                         //else {
                         //  $acquired_record['state'] = (isset($active_records[$nid][$realm][$gid]) ? 'illegitimate' : 'ignored');
                         //}
                         $all_records[] = $acquired_record + array('nid' => $nid);
                     }
                 }
             }
         }
         // Fill in the table rows.
         $rows = array();
         $error_count = 0;
         foreach ($all_records as $record) {
             $row = new \stdClass();
             $row->nid = $record['nid'];
             $row->title = $record['#title'];
             $row->priority = $record['priority'];
             $row->state = array('data' => $states[$record['state']][0], 'title' => $states[$record['state']][2]);
             $row->realm = $record['realm'];
             $row->gid = $record['gid'];
             $row->grant_view = $record['grant_view'];
             $row->grant_update = $record['grant_update'];
             $row->grant_delete = $record['grant_delete'];
             $row->explained = implode('<br />', \Drupal::moduleHandler()->invokeAll('node_access_explain', $row));
             unset($row->title);
             if ($row->nid == 0 && $row->gid == 0 && $row->realm == 'all' && count($all_records) > 1) {
                 $row->state = array('data' => $states['unexpected'][0], 'title' => $states['unexpected'][2]);
                 $class = $states['unexpected'][1];
             } else {
                 $class = $states[$record['state']][1];
             }
             $row = (array) $row;
             foreach (array('view', 'update', 'delete') as $op) {
                 $row["grant_{$op}"] = array('data' => $row["grant_{$op}"]);
                 if ((isset($checked_grants[$record['nid']][$op][$record['realm']]) && in_array($record['gid'], $checked_grants[$record['nid']][$op][$record['realm']]) || $row['nid'] == 0 && $row['gid'] == 0 && $row['realm'] == 'all') && !empty($row["grant_{$op}"]['data']) && in_array($record['state'], $active_states)) {
                     $row["grant_{$op}"]['data'] .= '&prime;';
                     $row["grant_{$op}"]['title'] = t('This entry grants access to this node to this user.');
                 }
                 if (isset($record["grant_{$op}!"])) {
                     $row["grant_{$op}"]['data'] = $record["grant_{$op}!"] . '&gt;' . (!$row["grant_{$op}"]['data'] ? 0 : $row["grant_{$op}"]['data']);
                     $row["grant_{$op}"]['class'][] = 'error';
                     if ($class == 'ok') {
                         $row['state'] = array('data' => $states['wrong'][0], 'title' => $states['wrong'][2]);
                         $class = $states['wrong'][1];
                     }
                 }
             }
             $error_count += $class == 'error';
             $row['nid'] = array('data' => '<a href="#node-' . $record['nid'] . '">' . $row['nid'] . '</a>', 'title' => $record['#title']);
             if (empty($record['#module']) || strpos($record['realm'], $record['#module']) === 0) {
                 $row['realm'] = $record['realm'];
             } else {
                 $row['realm'] = array('data' => '(' . $record['#module'] . '::) ' . $record['realm'], 'title' => t("The '@module' module fails to adhere to the best practice of naming its realm(s) after itself.", array('@module' => $record['#module'])));
             }
             // Prepend information from the D7 hook_node_access_records_alter().
             $next_style = array();
             if (isset($record['history'])) {
                 $history = $record['history'];
                 if (($num_changes = count($history['changes']) - empty($history['current'])) > 0) {
                     $first_row = TRUE;
                     while (isset($history['original']) || $num_changes--) {
                         if (isset($history['original'])) {
                             $this_record = $history['original'];
                             $this_action = '[ Original by ' . $this_record['#module'] . ':';
                             unset($history['original']);
                         } else {
                             $change = $history['changes'][0];
                             $this_record = $change['record'];
                             $this_action = ($first_row ? '[ ' : '') . $change['op'] . ':';
                             array_shift($history['changes']);
                         }
                         $rows[] = array('data' => array('data' => array('data' => $this_action, 'style' => array('padding-bottom: 0;'))), 'style' => array_merge($first_row ? array() : array('border-top-style: dashed;', 'border-top-width: 1px;'), array('border-bottom-style: none;')));
                         $next_style = array('border-top-style: none;');
                         if (count($history['changes'])) {
                             $g = $this_record;
                             $rows[] = array('data' => array('v', $g['priority'], '', $g['realm'], $g['gid'], $g['grant_view'], $g['grant_update'], $g['grant_delete'], 'v'), 'style' => array('border-top-style: none;', 'border-bottom-style: dashed;'));
                             $next_style = array('border-top-style: dashed;');
                         }
                         $first_row = FALSE;
                     }
                 }
             }
             // Fix up the main row cells with the proper class (needed for Bartik).
             foreach ($row as $key => $value) {
                 if (!is_array($value)) {
                     $row[$key] = array('data' => $value);
                 }
                 $row[$key]['class'] = array($class);
             }
             // Add the main row.
             $will_append = empty($history['current']) && !empty($history['changes']);
             $rows[] = array('data' => array_values($row), 'class' => array($class), 'style' => array_merge($next_style, $will_append ? array('border-bottom-style: none;') : array()));
             // Append information from the D7 hook_node_access_records_alter().
             if ($will_append) {
                 $last_change = end($history['changes']);
                 $rows[] = array('data' => array('data' => array('data' => $last_change['op'] . ' ]', 'style' => array('padding-top: 0;'))), 'style' => array('border-top-style: none;'));
             }
         }
         foreach ($rows as $i => $row) {
             $rows[$i] = self::format_row($row);
         }
         $output[] = array('#theme' => 'table', '#header' => $headers, '#rows' => $rows, '#attributes' => array('class' => array('system-status-report'), 'style' => 'text-align: left;'));
         $output[] = array('#theme' => 'form_element', '#description' => t('(Some of the table elements provide additional information if you hover your mouse over them.)'));
         if ($error_count > 0) {
             $variables['!Rebuild_permissions'] = '<a href="' . url('admin/reports/status/rebuild') . '">' . $tr('Rebuild permissions') . '</a>';
             $output[] = array('#prefix' => "\n<span class=\"error\">", '#markup' => t("You have errors in your !na table! You may be able to fix these for now by running !Rebuild_permissions, but this is likely to destroy the evidence and make it impossible to identify the underlying issues. If you don't fix those, the errors will probably come back again. <br /> DON'T do this just yet if you intend to ask for help with this situation.", $variables), '#suffix' => "</span><br />\n");
         }
         // Explain whether access is granted or denied, and why
         // (using code from node_access()).
         $tr = 't';
         array_shift($nids);
         // Remove the 0.
         $accounts = array();
         $variables += array('!username' => '<em class="placeholder">' . $user->getDisplayName() . '</em>', '%uid' => $user->id());
         if (\Drupal::currentUser()->hasPermission('bypass node access')) {
             $variables['%bypass_node_access'] = $tr('bypass node access');
             $output[] = array('#markup' => t('!username has the %bypass_node_access permission and thus full access to all nodes.', $variables), '#suffix' => '<br />&nbsp;');
         } else {
             $variables['!list'] = '<div style="margin-left: 2em">' . self::get_grant_list($grants_data['view']) . '</div>';
             $variables['%access'] = 'view';
             $output[] = array('#prefix' => "\n<div style='text-align: left' title='" . t('These are the grants returned by hook_node_grants() for this user.') . "'>", '#markup' => t('!username (user %uid) can use these grants (if they are present above) for %access access: !list', $variables), '#suffix' => "</div>\n");
             $accounts[] = $user;
         }
         if (isset($single_nid) && !$user_block_active) {
             // Only for single nodes.
             if (\Drupal::currentUser()->isAuthenticated()) {
                 $accounts[] = User::load(0);
                 // Anonymous, too.
             }
             foreach ($accounts as $account) {
                 $nid_items = array();
                 foreach ($nids as $nid) {
                     $op_items = array();
                     foreach (array('create', 'view', 'update', 'delete') as $op) {
                         $explain = self::explainAccess($op, Node::load($nid), $account);
                         $op_items[] = "<div style='width: 5em; display: inline-block'>" . t('%op:', array('%op' => $op)) . ' </div>' . $explain[2];
                     }
                     $nid_items[] = array('#theme' => 'item_list', '#items' => $op_items, '#type' => 'ul', '#prefix' => t('to node !nid:', array('!nid' => l($nid, 'node/' . $nid))) . "\n<div style='margin-left: 2em'>", '#suffix' => '</div>');
                 }
                 if (count($nid_items) == 1) {
                     $account_items = $nid_items[0];
                 } else {
                     $account_items = array('#theme' => 'item_list', '#items' => $nid_items, '#type' => 'ul', '#prefix' => "\n<div style='margin-left: 2em'>", '#suffix' => '</div>');
                 }
                 $variables['!username'] = '******' . theme('username', array('account' => $account)) . '</em>';
                 $output[] = array('#prefix' => "\n<div style='text-align: left'>", '#type' => 'item', 'lead-in' => array('#markup' => t("!username has the following access", $variables) . ' '), 'items' => $account_items, '#suffix' => "\n</div>\n");
             }
         }
     }
     return $output;
 }
Esempio n. 19
0
 /**
  * Trims the field down to the specified length.
  *
  * @param array $alter
  *   The alter array of options to use.
  *     - max_length: Maximum length of the string, the rest gets truncated.
  *     - word_boundary: Trim only on a word boundary.
  *     - ellipsis: Show an ellipsis (…) at the end of the trimmed string.
  *     - html: Make sure that the html is correct.
  *
  * @param string $value
  *   The string which should be trimmed.
  *
  * @return string
  *   The trimmed string.
  */
 public static function trimText($alter, $value)
 {
     if (Unicode::strlen($value) > $alter['max_length']) {
         $value = Unicode::substr($value, 0, $alter['max_length']);
         if (!empty($alter['word_boundary'])) {
             $regex = "(.*)\\b.+";
             if (function_exists('mb_ereg')) {
                 mb_regex_encoding('UTF-8');
                 $found = mb_ereg($regex, $value, $matches);
             } else {
                 $found = preg_match("/{$regex}/us", $value, $matches);
             }
             if ($found) {
                 $value = $matches[1];
             }
         }
         // Remove scraps of HTML entities from the end of a strings
         $value = rtrim(preg_replace('/(?:<(?!.+>)|&(?!.+;)).*$/us', '', $value));
         if (!empty($alter['ellipsis'])) {
             $value .= t('…');
         }
     }
     if (!empty($alter['html'])) {
         $value = Html::normalize($value);
     }
     return $value;
 }
Esempio n. 20
0
 /**
  * Matches an entity label to an input string.
  *
  * @param mixed $match
  *   The value to compare. This can be any valid entity query condition value.
  * @param string $match_operator
  *   The comparison operator.
  * @param string $label
  *   The entity label to match against.
  *
  * @return bool
  *   TRUE when matches, FALSE otherwise.
  */
 protected function matchLabel($match, $match_operator, $label)
 {
     // Always use a case-insensitive value.
     $label = Unicode::strtolower($label);
     switch ($match_operator) {
         case '=':
             return $label == $match;
         case '>':
             return $label > $match;
         case '<':
             return $label < $match;
         case '>=':
             return $label >= $match;
         case '<=':
             return $label <= $match;
         case '<>':
             return $label != $match;
         case 'IN':
             return array_search($label, $match) !== FALSE;
         case 'NOT IN':
             return array_search($label, $match) === FALSE;
         case 'STARTS_WITH':
             return strpos($label, $match) === 0;
         case 'CONTAINS':
             return strpos($label, $match) !== FALSE;
         case 'ENDS_WITH':
             return Unicode::substr($label, -Unicode::strlen($match)) === (string) $match;
         case 'IS NOT NULL':
             return TRUE;
         case 'IS NULL':
             return FALSE;
         default:
             // Invalid match operator.
             return FALSE;
     }
 }
 /**
  * FAPI validation of an individual field collection element.
  */
 public static function validate($element, FormStateInterface $form_state, $form)
 {
     $field_parents = $element['#field_parents'];
     $field_name = $element['#field_name'];
     $field_state = static::getWidgetState($field_parents, $field_name, $form_state);
     $field_collection_item = $field_state['field_collection_item'][$element['#delta']];
     $display = entity_get_form_display('field_collection_item', $field_name, 'default');
     $display->extractFormValues($field_collection_item, $element, $form_state);
     // Now validate required elements if the entity is not empty.
     if (!$field_collection_item->isEmpty() && !empty($element['#field_collection_required_elements'])) {
         foreach ($element['#field_collection_required_elements'] as &$elements) {
             // Copied from \Drupal\Core\Form\FormValidator::doValidateForm().
             // #1676206: Modified to support options widget.
             if (isset($elements['#needs_validation'])) {
                 $is_empty_multiple = !count($elements['#value']);
                 $is_empty_string = is_string($elements['#value']) && Unicode::strlen(trim($elements['#value'])) == 0;
                 $is_empty_value = $elements['#value'] === 0;
                 $is_empty_option = isset($elements['#options']['_none']) && $elements['#value'] == '_none';
                 if ($is_empty_multiple || $is_empty_string || $is_empty_value || $is_empty_option) {
                     if (isset($elements['#required_error'])) {
                         $form_state->setError($elements, $elements['#required_error']);
                     } else {
                         if (isset($elements['#title'])) {
                             $form_state->setError($elements, t('!name field is required.', array('!name' => $elements['#title'])));
                         } else {
                             $form_state->setError($elements);
                         }
                     }
                 }
             }
         }
     }
     // Only if the form is being submitted, finish the collection entity and
     // prepare it for saving.
     if ($form_state->isSubmitted() && !$form_state->hasAnyErrors()) {
         // Load initial form values into $item, so any other form values below the
         // same parents are kept.
         $field = NestedArray::getValue($form_state->getValues(), $element['#parents']);
         // Set the _weight if it is a multiple field.
         if (isset($element['_weight']) && $form[$field_name]['widget']['#cardinality_multiple']) {
             $field['_weight'] = $element['_weight']['#value'];
         }
         // Put the field collection field in $field['field_collection_item'], so
         // it is saved with the host entity via FieldCollection->preSave() / field
         // API if it is not empty.
         $field['field_collection_item'] = $field_collection_item;
         $form_state->setValue($element['#parents'], $field);
     }
 }
Esempio n. 22
0
 /**
  * Performs validation of elements that are not subject to limited validation.
  *
  * @param array $elements
  *   An associative array containing the structure of the form.
  * @param \Drupal\Core\Form\FormStateInterface $form_state
  *   The current state of the form. The current user-submitted data is stored
  *   in $form_state->getValues(), though form validation functions are passed
  *   an explicit copy of the values for the sake of simplicity. Validation
  *   handlers can also $form_state to pass information on to submit handlers.
  *   For example:
  *     $form_state->set('data_for_submission', $data);
  *   This technique is useful when validation requires file parsing,
  *   web service requests, or other expensive requests that should
  *   not be repeated in the submission step.
  */
 protected function performRequiredValidation(&$elements, FormStateInterface &$form_state)
 {
     // Verify that the value is not longer than #maxlength.
     if (isset($elements['#maxlength']) && Unicode::strlen($elements['#value']) > $elements['#maxlength']) {
         $form_state->setError($elements, $this->t('@name cannot be longer than %max characters but is currently %length characters long.', array('@name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title'], '%max' => $elements['#maxlength'], '%length' => Unicode::strlen($elements['#value']))));
     }
     if (isset($elements['#options']) && isset($elements['#value'])) {
         if ($elements['#type'] == 'select') {
             $options = OptGroup::flattenOptions($elements['#options']);
         } else {
             $options = $elements['#options'];
         }
         if (is_array($elements['#value'])) {
             $value = in_array($elements['#type'], array('checkboxes', 'tableselect')) ? array_keys($elements['#value']) : $elements['#value'];
             foreach ($value as $v) {
                 if (!isset($options[$v])) {
                     $form_state->setError($elements, $this->t('An illegal choice has been detected. Please contact the site administrator.'));
                     $this->logger->error('Illegal choice %choice in %name element.', array('%choice' => $v, '%name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title']));
                 }
             }
         } elseif ($elements['#type'] == 'select' && !$elements['#multiple'] && $elements['#required'] && !isset($elements['#default_value']) && $elements['#value'] === $elements['#empty_value']) {
             $elements['#value'] = NULL;
             $form_state->setValueForElement($elements, NULL);
         } elseif (!isset($options[$elements['#value']])) {
             $form_state->setError($elements, $this->t('An illegal choice has been detected. Please contact the site administrator.'));
             $this->logger->error('Illegal choice %choice in %name element.', array('%choice' => $elements['#value'], '%name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title']));
         }
     }
 }
Esempio n. 23
0
 /**
  * Prepares saving a new field definition.
  *
  * @param \Drupal\Core\Entity\EntityStorageInterface $storage
  *   The entity storage.
  *
  * @throws \Drupal\Core\Field\FieldException If the field definition is invalid.
  */
 protected function preSaveNew(EntityStorageInterface $storage)
 {
     $entity_manager = \Drupal::entityManager();
     $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
     // Assign the ID.
     $this->id = $this->id();
     // Field name cannot be longer than FieldStorageConfig::NAME_MAX_LENGTH characters.
     // We use Unicode::strlen() because the DB layer assumes that column widths
     // are given in characters rather than bytes.
     if (Unicode::strlen($this->getName()) > static::NAME_MAX_LENGTH) {
         throw new FieldException('Attempt to create a field storage with an name longer than ' . static::NAME_MAX_LENGTH . ' characters: ' . $this->getName());
     }
     // Disallow reserved field names.
     $disallowed_field_names = array_keys($entity_manager->getBaseFieldDefinitions($this->getTargetEntityTypeId()));
     if (in_array($this->getName(), $disallowed_field_names)) {
         throw new FieldException("Attempt to create field storage {$this->getName()} which is reserved by entity type {$this->getTargetEntityTypeId()}.");
     }
     // Check that the field type is known.
     $field_type = $field_type_manager->getDefinition($this->getType(), FALSE);
     if (!$field_type) {
         throw new FieldException("Attempt to create a field storage of unknown type {$this->getType()}.");
     }
     $this->module = $field_type['provider'];
     // Notify the entity manager.
     $entity_manager->onFieldStorageDefinitionCreate($this);
 }
Esempio n. 24
0
 /**
  * Renders a name component value.
  *
  * This function does not by default sanitize the output unless the markup
  * flag is set. If this is set, it runs the component through check_plain() and
  * wraps the component in a span with the component name set as the class.
  */
 public static function renderComponent($value, $component_key, $markup, $modifier = NULL)
 {
     if (empty($value) || !Unicode::strlen($value)) {
         return NULL;
     }
     switch ($modifier) {
         case 'initial':
             $value = Unicode::substr($value, 0, 1);
             break;
     }
     if ($markup) {
         return '<span class="' . SafeMarkup::checkPlain($component_key) . '">' . SafeMarkup::checkPlain($value) . '</span>';
     }
     return $value;
 }
Esempio n. 25
0
 /**
  * {@inheritdoc}
  */
 public function preSave(EntityStorageInterface $storage)
 {
     // Check if this is an entity bundle.
     if ($this->getEntityType()->getBundleOf()) {
         // Throw an exception if the bundle ID is longer than 32 characters.
         if (Unicode::strlen($this->id()) > EntityTypeInterface::BUNDLE_MAX_LENGTH) {
             throw new ConfigEntityIdLengthException("Attempt to create a bundle with an ID longer than " . EntityTypeInterface::BUNDLE_MAX_LENGTH . " characters: {$this->id}().");
         }
     }
 }
Esempio n. 26
0
 /**
  * Parses a word or phrase for parseQuery().
  *
  * Splits a phrase into words. Adds its words to $this->words, if it is not
  * already there. Returns a list containing the number of new words found,
  * and the total number of words in the phrase.
  */
 protected function parseWord($word)
 {
     $num_new_scores = 0;
     $num_valid_words = 0;
     // Determine the scorewords of this word/phrase.
     $split = explode(' ', $word);
     foreach ($split as $s) {
         $num = is_numeric($s);
         if ($num || Unicode::strlen($s) >= \Drupal::config('search.settings')->get('index.minimum_word_size')) {
             if (!isset($this->words[$s])) {
                 $this->words[$s] = $s;
                 $num_new_scores++;
             }
             $num_valid_words++;
         }
     }
     // Return matching snippet and number of added words.
     return array($num_new_scores, $num_valid_words);
 }
Esempio n. 27
0
 /**
  * Returns the string length.
  *
  * @return int
  *   The length of the string.
  */
 public function count()
 {
     return Unicode::strlen($this->string);
 }
Esempio n. 28
0
 /**
  * Tests multibyte strlen.
  *
  * @dataProvider providerStrlen
  * @covers ::strlen
  */
 public function testStrlen($text, $expected)
 {
     // Run through multibyte code path.
     Unicode::setStatus(Unicode::STATUS_MULTIBYTE);
     $this->assertEquals($expected, Unicode::strlen($text));
     // Run through singlebyte code path.
     Unicode::setStatus(Unicode::STATUS_SINGLEBYTE);
     $this->assertEquals($expected, Unicode::strlen($text));
 }
Esempio n. 29
0
 /**
  * {@inheritdoc}
  */
 public function preSave(EntityStorageInterface $storage)
 {
     // Check if this is an entity bundle.
     if ($this->getEntityType()->getBundleOf()) {
         // Throw an exception if the bundle ID is longer than 32 characters.
         if (Unicode::strlen($this->id()) > EntityTypeInterface::BUNDLE_MAX_LENGTH) {
             throw new ConfigEntityIdLengthException(SafeMarkup::format('Attempt to create a bundle with an ID longer than @max characters: @id.', array('@max' => EntityTypeInterface::BUNDLE_MAX_LENGTH, '@id' => $this->id())));
         }
     }
 }
Esempio n. 30
0
 /**
  * {@inheritdoc}
  */
 public function cleanString($string, array $options = array())
 {
     if (empty($this->cleanStringCache)) {
         // Generate and cache variables used in this method.
         $config = $this->configFactory->get('pathauto.settings');
         $this->cleanStringCache = array('separator' => $config->get('separator'), 'strings' => array(), 'transliterate' => $config->get('transliterate'), 'punctuation' => array(), 'reduce_ascii' => (bool) $config->get('reduce_ascii'), 'ignore_words_regex' => FALSE, 'lowercase' => (bool) $config->get('case'), 'maxlength' => min($config->get('max_component_length'), $this->aliasStorageHelper->getAliasSchemaMaxLength()));
         // Generate and cache the punctuation replacements for strtr().
         $punctuation = $this->getPunctuationCharacters();
         foreach ($punctuation as $name => $details) {
             $action = $config->get('punctuation.' . $name);
             switch ($action) {
                 case PathautoManagerInterface::PUNCTUATION_REMOVE:
                     $cache['punctuation'][$details['value']] = '';
                     $this->cleanStringCache;
                 case PathautoManagerInterface::PUNCTUATION_REPLACE:
                     $this->cleanStringCache['punctuation'][$details['value']] = $this->cleanStringCache['separator'];
                     break;
                 case PathautoManagerInterface::PUNCTUATION_DO_NOTHING:
                     // Literally do nothing.
                     break;
             }
         }
         // Generate and cache the ignored words regular expression.
         $ignore_words = $config->get('ignore_words');
         $ignore_words_regex = preg_replace(array('/^[,\\s]+|[,\\s]+$/', '/[,\\s]+/'), array('', '\\b|\\b'), $ignore_words);
         if ($ignore_words_regex) {
             $this->cleanStringCache['ignore_words_regex'] = '\\b' . $ignore_words_regex . '\\b';
             if (function_exists('mb_eregi_replace')) {
                 mb_regex_encoding('UTF-8');
                 $this->cleanStringCache['ignore_words_callback'] = 'mb_eregi_replace';
             } else {
                 $this->cleanStringCache['ignore_words_callback'] = 'preg_replace';
                 $this->cleanStringCache['ignore_words_regex'] = '/' . $this->cleanStringCache['ignore_words_regex'] . '/i';
             }
         }
     }
     // Empty strings do not need any processing.
     if ($string === '' || $string === NULL) {
         return '';
     }
     $langcode = NULL;
     if (!empty($options['language'])) {
         $langcode = $options['language']->getId();
     } elseif (!empty($options['langcode'])) {
         $langcode = $options['langcode'];
     }
     // Check if the string has already been processed, and if so return the
     // cached result.
     if (isset($this->cleanStringCache['strings'][$langcode][(string) $string])) {
         return $this->cleanStringCache['strings'][$langcode][(string) $string];
     }
     // Remove all HTML tags from the string.
     $output = Html::decodeEntities($string);
     $output = PlainTextOutput::renderFromHtml($output);
     // Optionally transliterate.
     if ($this->cleanStringCache['transliterate']) {
         // If the reduce strings to letters and numbers is enabled, don't bother
         // replacing unknown characters with a question mark. Use an empty string
         // instead.
         $output = $this->transliteration->transliterate($output, $langcode, $this->cleanStringCache['reduce_ascii'] ? '' : '?');
     }
     // Replace or drop punctuation based on user settings.
     $output = strtr($output, $this->cleanStringCache['punctuation']);
     // Reduce strings to letters and numbers.
     if ($this->cleanStringCache['reduce_ascii']) {
         $output = preg_replace('/[^a-zA-Z0-9\\/]+/', $this->cleanStringCache['separator'], $output);
     }
     // Get rid of words that are on the ignore list.
     if ($this->cleanStringCache['ignore_words_regex']) {
         $words_removed = $this->cleanStringCache['ignore_words_callback']($this->cleanStringCache['ignore_words_regex'], '', $output);
         if (Unicode::strlen(trim($words_removed)) > 0) {
             $output = $words_removed;
         }
     }
     // Always replace whitespace with the separator.
     $output = preg_replace('/\\s+/', $this->cleanStringCache['separator'], $output);
     // Trim duplicates and remove trailing and leading separators.
     $output = $this->getCleanSeparators($this->getCleanSeparators($output, $this->cleanStringCache['separator']));
     // Optionally convert to lower case.
     if ($this->cleanStringCache['lowercase']) {
         $output = Unicode::strtolower($output);
     }
     // Shorten to a logical place based on word boundaries.
     $output = Unicode::truncate($output, $this->cleanStringCache['maxlength'], TRUE);
     // Cache this result in the static array.
     $this->cleanStringCache['strings'][$langcode][(string) $string] = $output;
     return $output;
 }