0

I'm working with Drupal 8, but since this looks like a PHP problem to me, I'm asking it here and not on Drupal Answers.

The isset statement in the following code should return TRUE, but it does not:

/*
$specifier = 'field_google_hire_identifier‎'
$field_storage_definitions = array of objects
*/

var_dump(
  isset($field_storage_definitions[$specifier]),
  $specifier,
  $field_storage_definitions[$specifier],
  $field_storage_definitions
);
die;

Output on the screen: var_dump() output Other array elements... var_dump() output Other array elements...

Original output HTML: https://codepen.io/anon/pen/ZwRJdR

Array keys:

var_dump( array_keys($field_storage_definitions));

Output on the screen: array keys

I'm kind of clueless why isset($field_storage_definitions[$specifier] is FALSE and $field_storage_definitions[$specifier] is NULL

I uninstalled APC and disabled Opcache.

Orlando
  • 489
  • 7
  • 19
  • `$field_storage_definitions[$specifier]` is NULL - is it unexpected ? – Istiaque Ahmed Feb 12 '19 at 15:39
  • whish key you need to access ? – Yassine CHABLI Feb 12 '19 at 15:39
  • @IstiaqueAhmed yes, it should be a Drupal\field\Entity\FieldStorageConfig object, as shown in the screenshot. – Orlando Feb 12 '19 at 15:41
  • @MohammedYassineCHABLI the 'field_google_hire_identifier‎' key – Orlando Feb 12 '19 at 15:41
  • @Orlando, can't `$field_storage_definitions[$specifier]` be Null any way ? – Istiaque Ahmed Feb 12 '19 at 15:42
  • the "field_google_hire_identifier" is an object , and this object have protected value , to get those properties , use reflection , wanna see an example if that what you are looking for – Yassine CHABLI Feb 12 '19 at 15:43
  • @MohammedYassineCHABLI yes, it's an object, but I don't want to get any values from it. I only want to get the object. – Orlando Feb 12 '19 at 15:45
  • @Orlando, how do you get the value of `$field_storage_definitions[$specifier]` ? – Istiaque Ahmed Feb 12 '19 at 15:46
  • @IstiaqueAhmed I just use var_dump($field_storage_definitions[$specifier]); – Orlando Feb 12 '19 at 15:47
  • @Orlando, but how do you get the value of `$field_storage_definitions[$specifier]` ? Where does the value of `$field_storage_definitions` come from ? – Istiaque Ahmed Feb 12 '19 at 15:47
  • try with :$field_storage_definitions->{$specifier} – Yassine CHABLI Feb 12 '19 at 15:50
  • @IstiaqueAhmed $field_storage_definitions is an array generated by Drupal: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21EntityManager.php/function/EntityManager%3A%3AgetFieldStorageDefinitions/8.6.x But it looks like a valid array, so I don't think theres the problem. – Orlando Feb 12 '19 at 15:53
  • @Orlando, what is the full value of `$field_storage_definitions`? – Istiaque Ahmed Feb 12 '19 at 15:54
  • @MohammedYassineCHABLI $field_storage_definitions->{$specifier} is NULL – Orlando Feb 12 '19 at 15:57
  • 1
    @Orlando try run var_dump( array_keys($field_storage_definitions)); – Muhammad Azizol Aminuddin Feb 12 '19 at 16:04
  • @MuhammadAzizolAminuddin I've added it to the question. There is a 'field_google_hire_identifier‎' array key. But for some reason, it seems like it does not exist. – Orlando Feb 12 '19 at 16:13
  • 1
    Please check the **real** content of `$specifier` - according to the first dump of that variable, it contains a string with 31 characters, while the string `field_google_hire_identifier` obviously contains only 28 characters – Nico Haase Feb 12 '19 at 16:19
  • @NicoHaase oh, yes. That's a good point. But how can I get the real content of it? Shouldn't var_dump() return the real content? – Orlando Feb 12 '19 at 16:21
  • Well, where does it come from? Could it contain spaces, tabulators, line feeds,...? – Nico Haase Feb 12 '19 at 16:22
  • @NicoHaase It is passed a method argument: $nodesNotOnSourceEntityQuery->condition('field_google_hire_identifier‎', $sourceIds, 'NOT IN'); – Orlando Feb 12 '19 at 16:24
  • @NicoHaase executing the following `var_dump($specifier, 'field_google_hire_identifier‎');` returns: /app/web/core/lib/Drupal/Core/Entity/Query/Sql/Tables.php:102:string 'field_google_hire_identifier‎' (length=31) /app/web/core/lib/Drupal/Core/Entity/Query/Sql/Tables.php:102:string 'field_google_hire_identifier‎' (length=31) which is very odd, since 'field_google_hire_identifier‎' only has 28 characters – Orlando Feb 12 '19 at 16:27
  • 1
    Have you checked whether there are any hidden chars that a `var_dump` would not show? – Nico Haase Feb 12 '19 at 16:31
  • There is a [https://www.fileformat.info/info/unicode/char/200f/index.htm](right to left mark) at the end of the string: `var_dump($specifier, json_encode('field_google_hire_identifier‎'), preg_replace('/\p{C}+/u', "", 'field_google_hire_identifier‎'));` Output: string 'field_google_hire_identifier‎' (length=31) string '"field_google_hire_identifier\u200e"' (length=36) string 'field_google_hire_identifier' (length=28) [https://stackoverflow.com/questions/23130740/determining-and-removing-invisible-characters-from-a-string-in-php-e2808e](It is removed using this) – Orlando Feb 12 '19 at 16:41
  • 1
    @NicoHaase thanks a lot for your help. Now I know what the problem is, and what to look out for. The input string is fine, but somewhere on the way the right to left mark is added. Have to dig into it where this happens. But that is outside of the scope of this question. – Orlando Feb 12 '19 at 17:11

2 Answers2

2

There was a right to left mark at the end of the string that was used to select the array key. Since the string and the array key were not the same, the array key could not be retrieved.

The left to right mark can be removed using

preg_replace('/\p{C}+/u', "", $string)

See https://stackoverflow.com/a/23131396/6653862

You can check if a string contains invisible characters by using

json_encode($string)
Orlando
  • 489
  • 7
  • 19
-3

If I were you, I would try this:

$true_Array = (Array)$array_With_Object;
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129