Correct way to get field definitions for entity types I'm still pretty new to D8 so I'm getting to grips with the new way it deals with fields and the OOP way of doing it etc.
I've got a hook in my module which I'm trying to add a submit handler to different types of form submission (user registration, comment forms, webforms etc) and I'm trying to write something which will go through each of the admin added fields (eg, all fields that start with field_ ) and deal with each field type so that I can do something additional with it at the end
In pseudo terms
foreach ($fields as $field_name =&gt; $field) {
    if (substr($field_name, 0, 6) === &quot;field_&quot;) {
      switch ( //field type ) {
        case 'boolean':
          // if it's a boolean, do this
          break;
  
        case 'entity_reference':
          // if it's an entity reference, do this
          break;

        default:
          // for everything else, do this

          break;
      }
    }
  }

But where my switch statement is, I'm basically having to check against the field definition to determine what type of field I'm checking against, because if I just check against the submit handler answers, it only gives me the response.
Currently I'm getting the definitions by doing this
$definitions = \Drupal::service('entity_field.manager')-&gt;getFieldDefinitions('entity_type', 'bundle');

Is that the best method to do it? When I tried to run dpm() on this for a custom comment form type, it just seems to kill my site, not like a requirement error, but it just seems to have a memory leak when I try to do it or something.
Think this is an OOP question... so the correct answer is to switch by the type of the instance. In PHP you do this with &quot;instanceof&quot;, like here in MappingTest.php:
protected function filterNumericFields(&amp;$fields) {
    foreach ($this-&gt;view-&gt;field as $id =&gt; $field) {
      if (!($field instanceof NumericField)) {
        unset($fields[$id]);
      }
    }
  }

Note: dpm is bad with nested objects... you should set up a debugger like xdebug.
Another Note: Each content entity has a getFieldDefinitions method:
   /**
   * {@inheritdoc}
   */
  public function getFieldDefinitions() {
    if (!isset($this-&gt;fieldDefinitions)) {
      $this-&gt;fieldDefinitions = \Drupal::service('entity_field.manager')-&gt;getFieldDefinitions($this-&gt;entityTypeId, $this-&gt;bundle());
    }
    return $this-&gt;fieldDefinitions;
  }