studioespresso / craft3-seeder

Seeder is the easiest way to quickly create placeholder content while you're building out a website. Create your sections & fields and then let Seeder make entries for you.
https://plugins.craftcms.com/seeder
MIT License
28 stars 3 forks source link

FR: Add progress bar #4

Closed sjelfull closed 5 years ago

sjelfull commented 5 years ago

You can use the Console helper's start/update/endProgress methods.

Example:

public function generate( $section = null, $count ) {
        if(ctype_digit($section)) {
            $section = Craft::$app->sections->getSectionById( (int) $section );
        } else {
            $section = Craft::$app->sections->getSectionByHandle($section);
        }

        if(!$section) {
            echo "Section not found\n";
            return false;
        }
        $faker = Factory::create();

        $entryTypes = $section->getEntryTypes();
        $current = 1;
        $total = count($entryTypes) * $count;
        Console::startProgress($current, $count);

        foreach ( $entryTypes as $entryType ) {
            for ( $x = 1; $x <= $count; $x ++ ) {
                $current++;
                Console::updateProgress($current, $count);

                $typeFields = Craft::$app->fields->getFieldsByLayoutId( $entryType->getFieldLayoutId() );
                $entry      = new Entry( [
                    'sectionId' => (int) $section->id,
                    'typeId'    => $entryType->id,
                    'title'     => Seeder::$plugin->fields->Title(),
                ] );
                Craft::$app->getElements()->saveElement( $entry );
                $entry = Seeder::$plugin->seeder->populateFields( $typeFields, $entry );
                Craft::$app->getElements()->saveElement( $entry );
                $entry->id;

                Seeder::$plugin->seeder->saveSeededEntry($entry);

            }
        }

        Console::endProgress();

        return $section->name;

    }
janhenckens commented 5 years ago

This is neat, thanks for the suggestion @sjelfull! Adding it to all the things :)

janhenckens commented 5 years ago

Finally got around to releasing this, out in 3.0.1!