public function getIconDirectory(\ElggEntity $entity, $size = null, $directory = null) {

		$sizes = $this->getSizes($entity);
		if (isset($sizes[$size]['metadata_name'])) {
			$md_name = $sizes[$size]['metadata_name'];
			if ($entity->$md_name) {
				$directory = '';
			}
		}

		if ($directory === null) {
			$directory = $directory ? : $entity->icon_directory;
			if ($entity instanceof \ElggUser) {
				$directory = 'profile';
			} else if ($entity instanceof \ElggGroup) {
				$directory = 'groups';
			} else {
				$directory = $this->config->getDefaultIconDirectory();
			}
		}

		$directory = elgg_trigger_plugin_hook('entity:icon:directory', $entity->getType(), array(
			'entity' => $entity,
			'size' => $size,
				), $directory);

		return trim($directory, '/');
	}