3

Earlier I created an extended version of the Node class. I called it AbleNode and it extended BaseNode. In order to set all nodes to this node type I added this to my custom .module file

function ablecore_entity_type_build(&$entity_types){
    if(isset($entity_types['node'])){
        $entity_types['node']->setClass('Drupal\ablecore\Plugin\Node\AbleNode');
    }
}

This works fine but I'm not sure how to do this with an extended field type. Here is the code for my field class that is extending the BaseFileFieldItemList class

<?php
namespace Drupal\ablecore\Plugin\Field\FieldType\FileFieldItemList;
/**
 * @file AbleNode.php
 */
use Drupal\file\Plugin\Field\FieldType\FileFieldItemList as BaseFileFieldItemList;

class AbleFileFieldItemList extends BaseFileFieldItemList {
    public function style($image_style) {
        $image_style = \Drupal\image\Entity\ImageStyle::load($image_style);
        if (!empty($image_style)) {
            return $image_style->buildUrl($this->getEntity()->getFileuri());
        } else {
            return false;
        }
    }
}

I see how I was able to turn all nodes into AbleNodes, but I'm not sure how to make all FileFieldItemLists into AbleFileFieldItemLists. Is there a hook I can use to setClass on the field whenever I encounter it? Keep in mind that I'm not trying to create a brand new field, I just want to give more functionality to existing field types.

2

This is untested, but I'm pretty sure you just need to change the list class for the field types using hook_field_info_alter():

function ablecore_field_info_alter(&$info) {
  foreach ($info as &$field) {
    if ($field['list_class'] == '\Drupal\file\Plugin\Field\FieldType\FileFieldItemList') {
      $field['list_class'] = '\Drupal\ablecore\Plugin\Field\FieldType\AbleFileFieldItemList';
    }
  }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.