Uncategorized

Introducing Drupal 8’s Entity Validation API

Photo
Drupal 8 comes with plenty of new features: the high visibility ones, like CKEditor or Views in core, and those less obvious but equally pivotal to Drupal 8’s strength and flexibility, like the Entity Validation API.

Never heard of it? You’re not alone – much of the fanfare around Drupal 8 is devoted to the shiny parts. But under the hood, rock solid developer APIs like the Entity Validation API are what will make Drupal 8 a pleasure to work with for client projects and contributed modules alike.

So what is this Entity Validation API and why should you care?

For Those Who Came in Late

In Drupal versions up to and including Drupal 7, any validation was done in the Form API. Consider the Comment entity, provided by the Comment module. There is a lot of validation relating to comments, such as:

  • If the comment is being updated, we confirm the timestamp is a valid date.
  • If the comment is being updated and the username is changed, we confirm the username is valid.
  • If the comment is anonymous, we validate that the name used for the comment doesn’t match an existing username.
  • If the comment is anonymous, we confirm that the e-mail address entered is valid.
  • If the comment is anonymous, we confirm that the homepage is a valid URL.

In Drupal 7 and earlier all of this happens in the comment form validation logic, in comment_form_validate().

The issue here is that this validation is tied to a form submission. If you’re saving a comment via some other method, then you have to duplicate all this logic to ensure you don’t end up with invalid comment entities. Common alternate methods include:

  1. using Rules;
  2. using a Restful, Services, or Rest WS endpoint;
  3. programmatically saving via custom code;
  4. using a custom comment form.

The same scenario is repeated for Nodes, Users, Taxonomy terms, and custom Blocks (which aren’t entities per se in Drupal 7, but the story is the same).

It’s Like an Onion, or Maybe a Layer Cake

But before we can talk about Drupal 8’s Entity Validation API, we need to go over some background on the Entity API itself.

In Drupal 7, entities are \StdClassobjects; accessing field values depends on the entity and the field. There is no real unification. For example, $node->title is a string, while $node->field_tags is an array.

And so, in Drupal 7 you might see things like this:

$node->field_make_it_stop['LANGUAGE_NONE'][0]['wtf_bbq'];

In Drupal 8, the Entity Field API brings unified access to field properties and first-class objects for each entity type. So in Drupal 8, you see consistency like this:

$node->field_rainbows->value;
$node->title->value;

When you work with fields and entities in Drupal 8, you’re likely to interact with a suite of interfaces that comprise the API.

Key Interfaces in Drupal 8 Entity Field API

Let’s start with ContentEntityInterface, the overarching interface that content entities in Drupal 8 implement. (Node, Comment, Taxonomy Term, and BlockContent, among others, implement this interface.)

Each field and each property on a content entity is an instance of FieldItemListInterface. Even fields like the node title are lists, which just contain a single value.

Each FieldItemListInterface consists of one or more FieldItemInterface objects.

At the lowest level of the API, each FieldItemInterface is comprised of one or more DataDefinitionInterface objects that make up the properties or columns in each item value.

View More 2001
View More 2002
View More 2003
View More 2004
View More 2005
View More 2006
View More 2007
View More 2008
View More 2009
View More 2010
View More 2011
View More 2012
View More 2013
View More 2014
View More 2015
View More 2016
View More 2017
View More 2018
View More 2019
View More 2020
View More 2021
View More 2022
View More 2023
View More 2024
View More 2025
View More 2026
View More 2027
View More 2028
View More 2029
View More 2030
View More 2031
View More 2032
View More 2033
View More 2034
View More 2035
View More 2036
View More 2037
View More 2038
View More 2039
View More 2040
View More 2041
View More 2042
View More 2043
View More 2044
View More 2045
View More 2046
View More 2047
View More 2048
View More 2049
View More 2050
View More 2051
View More 2052
View More 2053
View More 2054
View More 2055
View More 2056
View More 2057
View More 2058
View More 2059
View More 2060
View More 2061
View More 2062
View More 2063
View More 2064
View More 2065
View More 2066
View More 2067
View More 2068
View More 2069
View More 2070
View More 2071
View More 2072
View More 2073
View More 2074
View More 2075
View More 2076
View More 2077
View More 2078
View More 2079
View More 2080
View More 2081
View More 2082
View More 2083
View More 2084
View More 2085
View More 2086
View More 2087
View More 2088
View More 2089
View More 2090
View More 2091
View More 2092
View More 2093
View More 2094
View More 2095
View More 2096
View More 2097
View More 2098
View More 2099
View More 2100
View More 2101
View More 2102
View More 2103
View More 2104
View More 2105
View More 2106
View More 2107
View More 2108
View More 2109
View More 2110
View More 2111
View More 2112
View More 2113
View More 2114
View More 2115
View More 2116
View More 2117
View More 2118
View More 2119
View More 2120
View More 2121
View More 2122
View More 2123
View More 2124
View More 2125
View More 2126
View More 2127
View More 2128
View More 2129
View More 2130
View More 2131
View More 2132
View More 2133
View More 2134
View More 2135
View More 2136
View More 2137
View More 2138
View More 2139
View More 2140
View More 2141
View More 2142
View More 2143
View More 2144
View More 2145
View More 2146
View More 2147
View More 2148
View More 2149
View More 2150
View More 2151
View More 2152
View More 2153
View More 2154
View More 2155
View More 2156
View More 2157
View More 2158
View More 2159
View More 2160
View More 2161
View More 2162
View More 2163
View More 2164
View More 2165
View More 2166
View More 2167
View More 2168
View More 2169
View More 2170
View More 2171
View More 2172
View More 2173
View More 2174
View More 2175
View More 2176
View More 2177
View More 2178
View More 2179
View More 2180
View More 2181
View More 2182
View More 2183
View More 2184
View More 2185
View More 2186
View More 2187
View More 2188
View More 2189
View More 2190
View More 2191
View More 2192
View More 2193
View More 2194
View More 2195
View More 2196
View More 2197
View More 2198
View More 2199
View More 2200
View More 2201
View More 2202
View More 2203
View More 2204
View More 2205
View More 2206
View More 2207
View More 2208
View More 2209
View More 2210
View More 2211
View More 2212
View More 2213
View More 2214
View More 2215
View More 2216
View More 2217
View More 2218
View More 2219
View More 2220
View More 2221
View More 2222
View More 2223
View More 2224
View More 2225
View More 2226
View More 2227
View More 2228
View More 2229
View More 2230
View More 2231
View More 2232
View More 2233
View More 2234
View More 2235
View More 2236
View More 2237
View More 2238
View More 2239
View More 2240
View More 2241
View More 2242
View More 2243
View More 2244
View More 2245
View More 2246
View More 2247
View More 2248
View More 2249
View More 2250
View More 2251
View More 2252
View More 2253
View More 2254
View More 2255
View More 2256
View More 2257
View More 2258
View More 2259
View More 2260
View More 2261
View More 2262
View More 2263
View More 2264
View More 2265
View More 2266
View More 2267
View More 2268
View More 2269
View More 2270
View More 2271
View More 2272
View More 2273
View More 2274
View More 2275
View More 2276
View More 2277
View More 2278
View More 2279
View More 2280
View More 2281
View More 2282
View More 2283
View More 2284
View More 2285
View More 2286
View More 2287
View More 2288
View More 2289
View More 2290
View More 2291
View More 2292
View More 2293
View More 2294
View More 2295
View More 2296
View More 2297
View More 2298
View More 2299
View More 2300
View More 2301
View More 2302
View More 2303
View More 2304
View More 2305
View More 2306
View More 2307
View More 2308
View More 2309
View More 2310
View More 2311
View More 2312
View More 2313
View More 2314
View More 2315
View More 2316
View More 2317
View More 2318
View More 2319
View More 2320
View More 2321
View More 2322
View More 2323
View More 2324
View More 2325
View More 2326
View More 2327
View More 2328
View More 2329
View More 2330
View More 2331
View More 2332
View More 2333
View More 2334
View More 2335
View More 2336
View More 2337
View More 2338
View More 2339
View More 2340
View More 2341
View More 2342
View More 2343
View More 2344
View More 2345
View More 2346
View More 2347
View More 2348
View More 2349
View More 2350
View More 2351
View More 2352
View More 2353
View More 2354
View More 2355
View More 2356
View More 2357
View More 2358
View More 2359
View More 2360
View More 2361
View More 2362
View More 2363
View More 2364
View More 2365
View More 2366
View More 2367
View More 2368
View More 2369
View More 2370
View More 2371
View More 2372
View More 2373
View More 2374
View More 2375
View More 2376
View More 2377
View More 2378
View More 2379
View More 2380
View More 2381
View More 2382
View More 2383
View More 2384
View More 2385
View More 2386
View More 2387
View More 2388
View More 2389
View More 2390
View More 2391
View More 2392
View More 2393
View More 2394
View More 2395
View More 2396
View More 2397
View More 2398
View More 2399
View More 2400
View More 2401
View More 2402
View More 2403
View More 2404
View More 2405
View More 2406
View More 2407
View More 2408
View More 2409
View More 2410
View More 2411
View More 2412
View More 2413
View More 2414
View More 2415
View More 2416
View More 2417
View More 2418
View More 2419
View More 2420
View More 2421
View More 2422
View More 2423
View More 2424
View More 2425
View More 2426
View More 2427
View More 2428
View More 2429
View More 2430
View More 2431
View More 2432
View More 2433
View More 2434
View More 2435
View More 2436
View More 2437
View More 2438
View More 2439
View More 2440
View More 2441
View More 2442
View More 2443
View More 2444
View More 2445
View More 2446
View More 2447
View More 2448
View More 2449
View More 2450
View More 2451
View More 2452
View More 2453
View More 2454
View More 2455
View More 2456
View More 2457
View More 2458
View More 2459
View More 2460
View More 2461
View More 2462
View More 2463
View More 2464
View More 2465
View More 2466
View More 2467
View More 2468
View More 2469
View More 2470
View More 2471
View More 2472
View More 2473
View More 2474
View More 2475
View More 2476
View More 2477
View More 2478
View More 2479
View More 2480
View More 2481
View More 2482
View More 2483
View More 2484
View More 2485
View More 2486
View More 2487
View More 2488
View More 2489
View More 2490
View More 2491
View More 2492
View More 2493
View More 2494
View More 2495
View More 2496
View More 2497
View More 2498
View More 2499
View More 2500

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button